Make \par work inside command arguments
Some remarks:
"Long" arguments with paragraphs (
\par
tokens) can only be used for macros, which are defined with\long
:\long\def\macro
. This is the default for\newcommand
.\setpage
needs to be defined this way.\callpage
, however, should not allow\par
tokens, which are forbidden in\csname
. It can be defined by\def
or the star form\newcommand*
.Since
\makeatletter
is active, the following example uses\@namedef
and\@nameuse
to replace the lengthy expressions with\csname
with a macro with a more understandable macro name.Line ends are usually converted to spaces by TeX, if not ignored after command sequence names, for instance. In vertical mode, they do not harm, but they will not be ignored in horizontal mode. The following example avoids this by commenting the line ends, when needed.
Full example:
\documentclass[12pt,a4paper]{article}
\usepackage{blindtext}
\makeatletter
%%%%makes a \page_#1 which expands to #2
\newcommand{\setpage}[2]{%
\@namedef{page_#1}{%
\newpage
#2%
\clearpage
}%
}
%%%%call the pages
\newcommand*{\callpage}[1]{%
\@nameuse{page_#1}%
}
%%%%define pre-defined pages
\setpage{1}{insert Seite \par
\blindtext
}
\setpage{2}{insert Seite2 % note the implicit \par here
}
\makeatother
\begin{document}
Text
\callpage{1}
\callpage{2}
Wieder Text
\end{document}
You're wrong in thinking that \def
is shorter. Maybe it looks shorter by number of characters, but it can save you from pulling your hair when some weird error shows up.
Suppose you load a package you use only some features of, but that this package uses a macro named \setpage
as part of its working when the feature you exploit is called.
Can you see what will go wrong?
Using \newcommand
, in this case, will tell you that the name is taken and you'll just say “OK, let me change the name”.
Correct definitions follow.
\newcommand\setpage[2]{%
\expandafter\def\csname glanz@page@#1\endcsname{%
\newpage
#2%
\clearpage
}%
}
\newcommand\callpage[1]{%
\csname glanz@page@#1\endcsname
}
Using @
is safer than _
(some packages make _
active and your usage could break). Also adding a prefix will emulate a “namespace”, minimizing the risk that you redefine a macro already taken.
Note the %
for protecting end-of-lines.
The macros defined with \newcommand
are automatically \long
, so they allow \par
in their arguments.