TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 '14 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.

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 '14 at 0:35
@tohecz Alright, I will try to provide an MWE when I have time. – Superbest Feb 3 '14 at 0:53

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 ...}%
    ... the description of what this macro does goes here ...

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


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



        This macro makes the text parameter bold.

        This macros makes the text parameter bold, and also in blue.

        This macros makes the text parameter bold, italicized, and also in red.



        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



Make \Bold{this bold.}

And in color: \BoldBlue{blue.}

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


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



\input{preamble} % define your macros


Use macro \foo{}.

\section{Commands I used} % document your macros



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


%% create an environment for theorems


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


share|improve this answer
Have you tested that this actually works? Also, please post a complete compilable code example. – Peter Grill Feb 3 '14 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 '14 at 1:39

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