Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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 …

share|improve this answer
    
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: tex.stackexchange.com/questions/69649/… , 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
show 11 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.