0

I tried to generate a macro for fixed content, but it's not working and I was wondering why. This is my code:

\documentclass{article}

\begin{document}

% this is working and puts A at the bottom of the page
\makeatletter
\def\@oddhead{%
\setlength\unitlength{1mm}%
\begin{picture}(0,0)%
\put(120,-250){\fbox{A}}%
\end{picture}\hfill}
\makeatother

% needed that page is not empty
test

% define a macro
\newcommand{\generateFixBox}{
\makeatletter
\def\@oddhead{%
\setlength\unitlength{1mm}%
\begin{picture}(0,0)%
\put(120,-250){\fbox{B}}%
\end{picture}\hfill}
 \makeatother
}

% apply macro, but not working?
\generateFixBox

\end{document}

The Latex error logs are confusing. I found the instructions for generating a fixed element here.

bersling
  • 423
  • 1
    Your code is missing a closing bracket, at \put(120,-250){\fbox{B}%, so it should be \put(120,-250){\fbox{B}}%. Is that your desired output? – Alenanno May 24 '16 at 17:30
  • ah thanks for spotting that, but this is actually not what i meant. I fixed the bracket, but only "A" is printed but not "B". Do you know why this is? – bersling May 24 '16 at 18:02

2 Answers2

3

The problem is the catcode of @:

\newcommand{\generateFixBox}{
  \makeatletter
  \def\@oddhead{%
    \setlength\unitlength{1mm}%
    \begin{picture}(0,0)%
      \put(120,-250){\fbox{B}}%
    \end{picture}\hfill}
   \makeatother
}

At definition time, \makeatletter is not executed, but the definition text is already tokenized. Therefore the input text \@oddhead becomes \@, o, d, d, h, e, a, d.

\makeatletter and \makeatother should be moved outside the definition:

\makeatletter
\newcommand{\generateFixBox}{%
  \def\@oddhead{...}%
}
\makeatother
Heiko Oberdiek
  • 271,626
2

there are several problems with your code.

first, you need \makeatletter and \makeatother at the outer level so that the @ is properly recognized in the command names. and then, these are not needed inside the definition of \generateFixBox.

second, in the definition of \generateFixBox, a closing brace is missing to complete {\fbox{B}.

the result gives a replacement definition that is properly recognized.

in the following code, i've changed the vertical location of the \fbox (it was disappearing off the bottom of the page, so i raised it a bit).

\documentclass{article}

\begin{document}

% this is working and puts A at the bottom of the page
\makeatletter
\def\@oddhead{%
\setlength\unitlength{1mm}%
\begin{picture}(0,0)%
%\put(120,-250){\fbox{A}}%
\put(120,-200){\fbox{A}}%
\end{picture}\hfill}
\makeatother

% needed that page is not empty
test

% define a macro
\makeatletter
\newcommand{\generateFixBox}{
%\makeatletter
\def\@oddhead{%
\setlength\unitlength{1mm}%
\begin{picture}(0,0)%
%\put(120,-250){\fbox{B}%
\put(120,-200){\fbox{B}}%
\end{picture}\hfill}
% \makeatother
}
\makeatother

% apply macro, but not working?
\generateFixBox

\end{document}