1

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?

Miles
  • 113
  • 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. https://gist.github.com/mlsteele/6487153 – Miles Sep 08 '13 at 18:21

1 Answers1

1

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).

Gonzalo Medina
  • 505,128
  • 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 08 '13 at 02: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 08 '13 at 02: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 08 '13 at 02: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 08 '13 at 02:22