4

I am making my own document class in LaTeX but I have a problem on how will I make this title appear automatically when my class is used.

example title

Should I code it this way?

\AtBeginDocument{%
   \vspace*{-0.4in}\noindent
   {\Large\bfseries School name here} \\
   {\large\sffamily college name here} \\[0.2in]
   {\Large\sffamily department name here} \\[0.2in]
   {\Large\sffamily Subjectcode, subject name here} \\[0.2in]
   {\Large\sffamily school year here} \\[0.2in]
   \vspace{0.2in}
}
David Carlisle
  • 757,742
Kayla
  • 1,655

1 Answers1

1

Yes. Something like that.

I browsed your profile and saw that you might be interested to write your own class or package so that whenever you use your class or package, your header will automatically be loaded. I can see where you're problem is coming from. It might be that you want to lessen the time you have to type a header like the one you posted whenever you want to make an exam or something like that (which may be your school test paper template or something like that).

You have several options to do this and I list two.

1. Put in a separate file all code that you use frequently then copy and paste as needed.

2. Create a package or a class file at which you have already been given some help but I would also like to point you to the post Style/class tutorials. There is also the related question Put an image header in \documentclass{letter}.

Using option 2 and the links found in the links I have posted here, I have come up with my own document class which automates the inclusion of a school header on the first page and none on the other pages for my class exams and school memos. For your specific problem, you can have a class file with the following contents.

  • If you want to have the header simply to appear on the first page without messing with the header, then you can do something like:

    \ProvidesClass{myclass}[2012/09/03 version 0.01 My exam class]
    \NeedsTeXFormat{LaTeX2e}[1996/06/01]%
    \PassOptionsToClass{\CurrentOption}{article}
    \ProcessOptions \relax
    
    \LoadClass{article}
    \RequirePackage[margin=1in]{geometry}
    \AtBeginDocument{
    \begin{center}
    \sffamily
    {\Large\textbf{School Name}}        {\large\textbf{Name of College}}\\
    {\large Name of Department}\\
    {\large Subject code, subject name}\\
    {\large SY 2012-2013}
    \end{center}
    \noindent Name: \makebox[3in]{\hrulefill} \hfill Section: \makebox[2in]{\hrulefill}\\
    }
    \endinput
    
  • If you want to want to use the top margin to save some space, then you can do something like:

    \ProvidesClass{myclass}[2012/09/03 version 0.01 My exam class]
    \NeedsTeXFormat{LaTeX2e}[1996/06/01]%               
    \PassOptionsToClass{\CurrentOption}{article}
    \ProcessOptions \relax
    
    \LoadClass{article}
    
    \RequirePackage[margin=1in]{geometry}
    \RequirePackage{fancyhdr}
    
    %% This sets the header of the first page of the letter
    \fancypagestyle{firstpage}{%
    \fancyhf{} % clear all six fields
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
    \fancyhead[C]{
    \parbox[t][]{4in}{
    \centering
    \sffamily
    {\Large\textbf{School Name}}\\
    {\large\textbf{Name of College}}\\
    {\large Name of Department}\\
    {\large Subject code, subject name}\\
    {\large SY 2012-2013}
    }}
    }
    \fancypagestyle{followingpage}{%
    \fancyhf{} % clear all six fields
    \renewcommand{\headrulewidth}{0pt}
    \renewcommand{\footrulewidth}{0pt}
    }
    \pagestyle{followingpage} % followingpage is the default page style
    \AtBeginDocument{\thispagestyle{firstpage} % the page style on the first page
    \geometry{headheight=1in,headsep=0.1in}
    \noindent Name: \makebox[3in]{\hrulefill} \hfill Section: \makebox[2in]{\hrulefill}\\
    }
    
    \endinput
    

Update: September 4. Below is an MWE for testing the classes I have posted here.

\documentclass{myclass}

\usepackage{lipsum}

\begin{document}

\lipsum[1-20]

\end{document}

Below is the output for the second class I posted. You can adjust the dimensions to suit your needs but the idea is there.

enter image description here

David Carlisle
  • 757,742
hpesoj626
  • 17,282
  • Hmmm... funny how some of the code still appears this way even if I have tried 4 spaces or highlight then ctrl-K – hpesoj626 Sep 03 '12 at 04:28
  • 1
    Code inside lists needs eight spaces and not just four. – Gonzalo Medina Sep 03 '12 at 04:36
  • @GonzaloMedina, thanks a lot. That explains it. I have now modified my answer to alter the spacing for the code. – hpesoj626 Sep 03 '12 at 04:41
  • @hpesoj626: Thank you so much. Its a really big help for beginners like me! But, can you show me a sample output of the second bullet you have posted. I can't really imagine what should it be look like. – Kayla Sep 03 '12 at 09:13
  • @Kayla, you are welcome. I have modified my answer. Welcome to TeX.sx! btw. Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. You can also accept an answer if you think it is the best answer. You may wait for 24 hours to pass before accepting one so that you can see if there will be some other posts which can answer your questions better. – hpesoj626 Sep 04 '12 at 01:12
  • hpesoj626: The thing is, i will provide the school name, college name etc. only on the first page. what do you mean sir of including this \fancypagestyle{followingpage}{% \fancyhf{} % clear all six fields \renewcommand{\headrulewidth}{0pt} \renewcommand{\footrulewidth}{0pt} } – Kayla Sep 04 '12 at 07:20
  • The fancyhdr package enables you to modify the contents of the six fields (left header, center header, right header, left footer, right footer, center footer). The use of \AtBeginDocument will put the header only on the first page. – hpesoj626 Sep 04 '12 at 07:29
  • @hpesoj626: and also, where should i declare new command if suddenly a user want to change the college name or the school name. is it after the \fancypagestyle{firstpage}{% command? – Kayla Sep 04 '12 at 07:30
  • @Kayla, the answer I gave was for fixed names of school, college, etc. You can define variables like \school, etc. If that is what you want, you can ask it as another question or edit your question to reflect this feature. – hpesoj626 Sep 04 '12 at 09:22
  • @hpesoj626: thanks a lot! i think i really need to raise another question. hehe :P thank u po! – Kayla Sep 04 '12 at 13:15
  • Is it possible to define this Name of college as command? example:
     \college{College of Commerce} and its place is same as the place where the old one is. At the place as the header.
    
    – Kayla Dec 12 '12 at 04:42
  • @kayla yes. Look at my explanation in my answer to your question at http://tex.stackexchange.com/questions/86550/a-line-after-text/86555#86555. Basically you just say \newcommand{\college}{College of Commerce} – hpesoj626 Dec 12 '12 at 05:02
  • is it \newcommand{\bt}[1]{\textbf{#1}}? I think this is how will i define it but how will I automatically place this as the header? – Kayla Dec 12 '12 at 05:15