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.

In a simple documentclass for homework assignments, how should I go about specifying a few attributes in addition to title and author in the topmatter?

\begin{document}
\acadclass{Linear Algebra}
\psetnum{2}
\title{Some Title}
\author{Joe Shmoe}

I would like this to render stuff like "Linear Algebra - Pset 2" in the header by evaluating \@acadclass - Pset \@psetnum. How can I declare these variables in the documentclass such that I could access them from \@acadclass?

share|improve this question
    
May I ask which document class do you use? If it is not a standard class can you give us a link? BTW: Welcome to TeX.SX! –  Kurt Sep 8 '13 at 1:06
    
Thanks :) It's a work in progress based on some other hw class I found on the net. Here's a gist of the current draft. It's pretty messy though. gist.github.com/mlsteele/6487153 –  Miles Sep 8 '13 at 18:21
add comment

1 Answer 1

up vote 2 down vote accepted

You can look for inspiration in the definition of \author in the LaTeX kernel:

\def\author#1{\gdef\@author{#1}}

An example with your commands:

\documentclass{article}
\usepackage{fancyhdr}
\pagestyle{fancy}

\makeatletter
\newcommand\acadclass[1]{\def\@acadclass{#1}}
\newcommand\psetnum[1]{\def\@psetnum{#1}}
\fancyhead[C]{\@acadclass--Pset~\@psetnum}
\makeatother

\acadclass{Linear Algebra}
\psetnum{2}


\acadclass{Linear Algebra}
\psetnum{2}
\title{Some Title}
\author{Joe Shmoe}

\begin{document}

test
\end{document}

An image of the resulting page showing the header:

enter image description here

To have also the header if \maketitle is used, add

\let\ps@plain\ps@fancy

inside

\makeatletter, \makeatother.

If these definitions are going to be used in a .cls file, you can do

\@ifpackageloaded{fancyhdr}{}{}{\RequirePackage{fancyhdr}}
\pagestyle{fancy}

\newcommand\acadclass[1]{\def\@acadclass{#1}}
\newcommand\psetnum[1]{\def\@psetnum{#1}}
\fancyhead[C]{\@acadclass--Pset~\@psetnum}

(in particular, in a .cls file, you do not use \makeatletter, \makeatother).

share|improve this answer
    
Thanks! I had some issues because my documentclass somehow relies on @ being a letter. So I just put \makeatother at the end of the documentclass. –  Miles Sep 8 '13 at 2:04
    
@Miles if you are talking about a .cls file, that indicates that you are using \makeatletter somewhere; if that is so, delete that command from your .cls file (and any possible \makeatother). \makeatletter, \makeatother inside a .cls file are dangerous. –  Gonzalo Medina Sep 8 '13 at 2:05
    
Yep, I'm talking about a .cls. Is it true then that .cls files are already in a scope where @ is a letter, and that in a .tex file the scope is automatically switched so that @ is an other? –  Miles Sep 8 '13 at 2:10
1  
@Miles yes; with the deafult settings, in a .cls (or a .sty) file @ is a letter (has catcode 11); in a .tex file @ is an other (has catcode 12). –  Gonzalo Medina Sep 8 '13 at 2:22
add comment

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.