4

Using my created document class will output an automated title header. My own created document class is entitled myclass. Is there a possible way for this not to become automated? I'd like to create a command for this but I don't really know how to. I simply want a command for each of this: "school name", next line is "dept name" and after which is command for the "college name". And this should appear at the upper center of the paper.

This is my code:

\AtBeginDocument
{
\begin{center}
\headrule
\par
\sffamily
\par
\par
{\Large\textbf{SchoolName}}\
{\large\textbf{College name}}\\
 {\large Dept name}\\
 \end{center}
 \noindent Name: \makebox[3in]{\hrulefill} \hfill Section: \makebox[2in]         
{\hrulefill}\\
\noindent Year\&Course: \makebox[2in]{\hrulefill} \hfill Date: \makebox[2in]    
{\hrulefill}\\
\begin{center}
Goodluck!
\end{center}

How could I create the command for those three? The code above, automatically prints the text in static. What if the user wants to modify the content of the text? Here is the sample PDF output of this .cls file code

sample image output

Moriambar
  • 11,466
Kayla
  • 1,655

1 Answers1

4

I guess there are two possible ways to add the header automatically and use user definable values. The first and easier way is to force the user to provide the information before typesetting the header, i.e. use \schoolname{My School} in the preamble:

\documentclass{myclass}

\schoolname{My School}% OK

\begin{document}
%\schoolname{My School}% ERR
Document content
\end{document}

This can be done by redefine \schoolname to caus en error when it is used after \begin{document}:

\ProvidesClass{myclass}

% base class
\LoadClassWithOptions{article}

% define the value macros
\newcommand{\@schoolname}{}
\newcommand{\schoolname}[1]{\gdef\@schoolname{#1}}
% ... etc ...

% provide the error message
\newcommand{\mycls@headerror}{%
    \ClassError{myclass}{Must be in preamble}{...}%
}

% define the hook
\AtBeginDocument{%
    {\centering\bfseries\sffamily
    \@schoolname
    \par}
    \vspace{\baselineskip}
    \hrule
    \vspace{\baselineskip}
    \let\schoolname\mycls@headerror
}

Prerequisites: none

Steps to create a new command:

  1. Define the macro to store the value \@<name>, e.g.

    \newcommand{\@schoolname}{}
    
  2. Define the macro to set the value \<name>, e.g.

    \newcommand{\schoolname}[1]{\gdef\@schoolname{#1}}
    
  3. Add the value (via \@<name>) to your header, e.g.

        {\centering\bfseries\sffamily
        \@schoolname
        \par}
    
  4. Redirect the setting macro (\<name>) to cause an error, after the header is typeset

        \let\schoolname\mycls@headerror
    

The second way is to give the values as class options. I used kvoptions to realize class options with values. The use must define the values via class options:

\documentclass[%
    schoolname={My School}
]{myclass}


\begin{document}
Document content
\end{document}

And thats how you can process it:

\ProvidesClass{myclass}

% load package and set it up
\RequirePackage{kvoptions}
\SetupKeyvalOptions{
    family=MC,% MC = my class
    prefix=my@% prefix to macros holding the values
}

% define the keys
\DeclareStringOption{schoolname}
% ... etc. ...

% Process the options
\ProcessKeyvalOptions*

% base class
\LoadClass{article}

% define the hook
\AtBeginDocument{%
    {\centering\bfseries\sffamily
    \my@schoolname% acces values by \<prefix>@<keyname>
    \par}
    \vspace{\baselineskip}
    \hrule
    \vspace{\baselineskip}
}

Prerequisites:

  1. Load kvoptions

  2. Define the <family> for the options an a <prefix> that is used for the value storing macros, e.g.

    \SetupKeyvalOptions{
        family=MC,
        prefix=my@
    }
    

    The names doesn’t matter but it is common to name the family in two or three upper case letters and to use an @ at the end of the prefix.

Steps to create a new command:

  1. Declare the option <name> (type: string), e.g.

    \DeclareStringOption{schoolname}
    
  2. Process all options with \ProcessOptionsX

  3. Use the values with \<prefix><name>, e.g.

        {\centering\bfseries\sffamily
        \my@schoolname
        \par}
    

For more information about kvoptions see it’s documentation …

Tobi
  • 56,353
  • i am creating my own document class myclass. A .cls file actually. Since this is a class file. I would like to know a way on how will i change the automated printing of school name, the college name, dept name to the pdf file by my code i had posted. What I think could be possible solution was make these things into command. So creating a command from \AtbeginDocument is I dont know. Could you guide me with this? – Kayla Feb 12 '13 at 16:24
  • i had posted above the sample pdf output compiled using my created class file (myclass.cls) – Kayla Feb 12 '13 at 16:38
  • @Kayla: I’m sorry seems like I get you wrong … I didn’t get the point of your question. Is it about the formatting/layout of your header or about getting the header automatically on the first (er every) page? – Tobi Feb 12 '13 at 16:57
  • I actually want it appear on the first page only. :) Please see my previous question about this: Here s the link: http://tex.stackexchange.com/questions/69649/creating-static-head-title-for-a-document-class , I want it to be dynamic for the reason of how if the user of my created class wanted the school name which is automatically printed, be changed. That's a know how. :( – Kayla Feb 12 '13 at 17:05
  • i should modify my code in my .cls and not to .tex for my class to provide a consistent class. – Kayla Feb 12 '13 at 17:09
  • I think I get the point and I can think about two ways to solve your problem.. I’ll edit my answer and provide the new examples after having dinner ;-) – Tobi Feb 12 '13 at 17:48
  • I added the first way but the second one taks some time to create the examples … – Tobi Feb 12 '13 at 17:52
  • @Kayla: I made two suggestions in my edited answer, hope they help you and answer your question …? – Tobi Feb 12 '13 at 20:09
  • Thanks! But Is there a possible way to reduce some spaces before the "My School" word? i think that would be nice if the spaces were consumed rightfully. The header place may be a better placement for the "My School" How will I do that? And i encounter the commands for the "college name" and "dept name", i cant define it right. – Kayla Feb 13 '13 at 12:38
  • I got it, i just modified the inches using a latex package. ow should i properly create the commands for the other two? Collegename and Dept name? I used the schoolname command as my guide but i dont know y is it not working. Help please. :( – Kayla Feb 13 '13 at 12:54
  • Hm … to creat other commands just do the same as I did for \schoolname (see my edit) … if it doesn’t work please add a MWE, showing the new problems, to your questions. Without its hard to answer. | The space before “My School” is the page margin, so you may want to change them with the geometry package … – Tobi Feb 13 '13 at 12:57
  • This was the .tex file \documentclass{Questionnaire} \schoolname{My School} \colname{College of AS}% \deptname{department of Theo} \begin{document} \end{document}

    and these are the commands i added to my .cls \newcommand{@schoolname}{} \newcommand{\schoolname}[1]{\gdef@schoolname{#1}} \newcommand{@colname}{} \newcommand{\colname}[1]{\gdef@colname{#1}} \newcommand{@deptname}{} \newcommand{\deptname}[1]{\gdef@deptname{#1}}

    when i compiled it, deptname and colname does not appear on the pdf file.

    – Kayla Feb 13 '13 at 13:11
  • I can’t see any problems without seeing the .cls file (or parts of it!) and a better description of the error. Pleas edit your qeustion, since code in the comment section is not really readable. – Tobi Feb 13 '13 at 13:13
  • sorry.. i read the instructions for this codes to look readable but i just cant perfectly make it here. I'll be editing it again. – Kayla Feb 13 '13 at 13:27
  • “does not appear on the pdf file.” -> You must add them to the \AtBeginDocument part too ;-) – Tobi Feb 13 '13 at 13:27
  • its done! Well sorry for this late rep, since lost my internet connection yesterday. Thanks a lot :) – Kayla Feb 14 '13 at 16:01