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.

I would like to collect certain \newcommands in an appendix, so that each one can have a section explaining itself. However, if I put this appendix at the end of the file, then I can't call the commands in the middle of my text because they won't be defined yet.

Is there any way to force LaTeX to scan a file and execute only the command definitions in it?

share|improve this question

closed as unclear what you're asking by Guido, Svend Tveskæg, Jesse, ChrisS, lockstep Feb 3 at 8:49

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

5  
Hi! As it stands now, your question is not quite understandable. Please, show a minimal (non-)working example, and show (again by a short example) what output exactly you want to achieve. –  yo' Feb 3 at 0:35
    
@tohecz Alright, I will try to provide an MWE when I have time. –  Superbest Feb 3 at 0:53

2 Answers 2

Here is a solution that should get you started. Place your \newcommands along with the description in MyPreamble.sty. The format of this file is

\newcommand*{\CommandName}{... command text ...}%
\begin{CommandDescription}
    ... the description of what this macro does goes here ...
\end{CommandDescription}

In the preamble you \include this file via \InputOnlyNewcommands{MyPreamble.sty}, which ignores the description of the macros, and only defines the \newcommands. In the main body you can use these macros as you want. At the end of the document you can have an appendix with these commands and their description via \InputIgnoringNewcommands{MyPreamble.sty}.

In the example below I define three commands along with a description of what they do.

enter image description here

Notes:

  • Not sure how to eliminate the quote at the beginning of the command name in the appendix.
  • The filecontents package was only used to package this example into a fully compilable example. It is not needed in your real example.
  • This only works with \newcommand. That is you can not use \def, \renewcommand, \providecommand, or any of the equivalent macros from the xparse package.

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{enumerate}
\usepackage{environ}

\usepackage{filecontents}
\begin{filecontents*}{MyPreamble.sty}
    \newcommand*{\Bold}[2][black]{\textbf{\textcolor{#1}{#2}}}%
    \begin{CommandDescription}
        This macro makes the text parameter bold.
    \end{CommandDescription}

    \newcommand*{\BoldBlue}[1]{\Bold[blue]{#1}}%
    \begin{CommandDescription}
        This macros makes the text parameter bold, and also in blue.
    \end{CommandDescription}

    \newcommand*{\BoldRedItalics}[1]{\textit{\Bold[red]{#1}}}%
    \begin{CommandDescription}
        This macros makes the text parameter bold, italicized, and also in red.
    \end{CommandDescription}
\end{filecontents*}

\NewEnviron{CommandDescription}[1]{}{}%

\newcommand{\InputOnlyNewcommands}[1]{%
    \RenewEnviron{CommandDescription}{}{}%
    \input{#1}%
}%

\newcommand{\InputIgnoringNewcommands}[1]{%
    \RenewEnviron{CommandDescription}{%
        \BODY\par
    }{%
    }%
    \RenewDocumentCommand{\newcommand}{%
        s%    #1 = optional star
        m%    #2 = macro name
        O{0}% #3 = number of parameters (optional, defaults to 1)
        o%    #4 = default value of optional parameter
        m%    #5 = code that was to be executed
    }{%
        \medskip\par\noindent%
        \Bold{\string##2:}
    }%
    \input{#1}%
}%


\InputOnlyNewcommands{MyPreamble.sty}

\begin{document}


\section{Body}
Make \Bold{this bold.}

And in color: \BoldBlue{blue.}

And in color with italics: \BoldRedItalics{red.}


\section{Appendix}
\InputIgnoringNewcommands{MyPreamble.sty}

\end{document}
share|improve this answer

I think I know what you want. Thanks to @PeterGrill and How display LaTeX code in LaTeX document? this now works:

% preamble.tex
%
% commands here, documented with comments

% \foo does whatever
\newcommand{\foo}{whatever}

Then

\documentclass{article}
\usepackage{verbatim}

\input{preamble} % define your macros

\begin{document}

\section{Contents}
Use macro \foo{}.

\section{Commands I used} % document your macros

   \verbatiminput{preamble}

\end{document}

You won't have LaTeX formatting for your macro documentation. Peter Grill's solution does that.

Edit: This question/answer is related to the naive construction of quines (see Self-replicating (La)TeX document). I use the idiom with \jobname to append TeX source for homework questions to the questions so that students have a template to start with:

% Math 370 hw2
%
\documentclass{article}
\pagestyle{empty}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{verbatim}

%% create an environment for theorems
\newtheorem*{thm}{Theorem}

\begin{document}

\begin{thm}
The amount by which the sum of the angles of a spherical triangle
exceeds $\pi$ is proportional to the area of the triangle.
\end{thm}

\verbatiminput{\jobname}

\end{document}
share|improve this answer
    
Have you tested that this actually works? Also, please post a complete compilable code example. –  Peter Grill Feb 3 at 1:11
    
@PeterGrill oops. My thinko. I will leave this faulty answer up for a while to see whether it's what the OP wants, and as a suggestion to someone who can do it right. I'll delete it soon. –  Ethan Bolker Feb 3 at 1:39

Not the answer you're looking for? Browse other questions tagged or ask your own question.