# Documentclass user macros

I wanted to create a documentclass for a complex template. I ran into the following problem:

1. I want to have user macros to store content in boxes which are then displayed by the template in the class code
2. I have to measure those boxes in order to set the dimensions of my layout
3. I would like my layout to be in the document class, and not in the document source file
4. I would like to be able to use user macros for storing the above-mentioned content in my document source file

Is that possible?

As a small example, let's pretend I want to create a frame using TikZ which would be big enough to accomodate user provided text . I would want something in the lines of:

documentclass:

...
\newsavebox{\titlebox}
\sbox{\titlebox}{\parbox{\5cm}{\UserProvidedText}}

\newlength\titleheight
\settototalheight\titleheight{\usebox{\titlebox}}

\begin{tikzpicture}[remember picture, overlay]
\draw (0,0) rectangle (5cm,\titleheight) {\UserProvidedText};
\end{tikzpicture}


document source:

%provide \UserProvidedText somehow


As I see it, documentclass needs to know about \UserProvidedText before the document source is itself read (I am presuming that TeX simply reads documentclass, and then goes on with reading the rest of the source).

-
You might want to rephrase your questions, especially 1. You want to have user macros which stores their content in boxes which are then displayed in certain points of the template by the class code, don't you? – Martin Scharrer Feb 28 '11 at 11:37
Yes, you are correct. – ipavlic Feb 28 '11 at 12:48
Could you be more specific? I think I understand the problem, but we can't tell what challenges you're encountering. Can you post a minimal example of what you've done so far and the undesired output? The answer to the question "Is this possible" is usually "Yes, if you do it right;" but that may not be the right question. – Matthew Leingang Feb 28 '11 at 13:00
I added a small code snippet with an explanation of what I am troubled with. I lack the knowledge to be able to explain it better without further questions or guidance. – ipavlic Feb 28 '11 at 13:31

The document class itself shouldn't produce any output, which is what your tikzpicture environment is attempting to do. One way to get around that is to save that code in an \AtBeginDocument command. For example, here is a class file, which I have named ipavlic.cls:

\LoadClass{article}
\renewcommand{\title}[1]{\gdef\@title{#1}}
\RequirePackage{tikz}

\AtBeginDocument{
\begin{tikzpicture}[remember picture, overlay]
\draw ([xshift=1cm]current page.west) node[rectangle,text width=5cm,rotate=90]  {\@title};
\end{tikzpicture}
}


The \LoadClass{article} command makes this class basically a “derived class” of the standard article.cls; probably a good idea to not start from scratch. :D The article class provides the \title command with the exact same functionality that I renewed it to; I just did that to show you what it does. It saves its input in a macro \@title (the @ sign is a just a convention for class/package-internal macros. It somewhat protects this text from being overwritten accidentally.) The \AtBeginDocument macro saves its input for execution right after \begin{document}.

Note also that it's TikZ nodes that contain text, not rectangles. And you don't need to save the title in a box then put it in a node—TikZ will probably take care of that for you.

Now here is a TeX file using this shiny new class.

\documentclass{ipavlic}
\title{TikZ Placement of Title}
\usepackage{lipsum}
\begin{document}
\lipsum
\end{document}


A disadvantage to this implementation is that because it relies on TikZ's remember picture mechanism, you have to run LaTeX twice to get the title in the right place.

Another method would be to alter the shipout routines which are executed at the end of every page. Your custom routine would place all of the rules and extra info outside of the regular type block. If it were me doing that I would use the pgfpages package but there are methods with less overhead I'm sure.

-
That's very helpful! I know the basics of TikZ but am calculating box sizes in order to draw a "table" grid (tex.stackexchange.com/questions/11937/…). Just to clarify, it is my understanding that TikZ needs two runs for remember picture. So if I want to use TikZ, I am forced to run it twice. Is that so? – ipavlic Feb 28 '11 at 14:14
@ipavlic: That's correct. Perhaps you can get away with using the anchors that TikZ attaches to its nodes to draw the grid lines. But in the end you will need to define the size of the type block based on the sizes of all your little nodes--which sounds like a headache to me. Knock yourself out, but if I were doing this I would fix the grid (maybe relative to the font size) and leave it up to the user to make sure his text fit in the grid. That's how real-world, paper, templated forms work. – Matthew Leingang Feb 28 '11 at 14:31