This document compiles fine.




But like, this feels wrong or immoral or something. Is there any specific reason this is a bad practice to define \newcommand macros inside the body of a document?

  • 8
    I do it if I want to define some ad hoc shorthand inside an environment (equations are often candidates for it), so it's forgotten at the end of the environment. Commands for general use should be in the preamble, so you can find them easily.
    – egreg
    Commented May 24, 2020 at 15:36
  • 7
    no it's fine, and sometimes necessary to come after definitions that occur \AtBeginDocument or at least this is an easy way to address such issues. But if it is a command with scope for the whole document that could be defined in the preamble it is more natural to do it there. Commented May 24, 2020 at 16:11
  • 2
    If you do not need the definition in the body of the document, or in a particular environment, it is as bad practice as far as the definitions code everywhere the text obfuscate the contents. It also forces you to remember in which part of the text you can use the macro or not (in practice = more errors by undefined control sequences). And it is also harder to reuse a set of custom macros from one document into another. But if your tolerance to edit text+code soups is high, no problem!
    – Fran
    Commented May 24, 2020 at 17:03

1 Answer 1


Yes, it is alright. I do it and it works.

In my use case, I create a large document (a PhD thesis) from smaller generated samples. Then for each sample I define certain parameters (for flexibility and uniformity). I enclose all of that in braces, as suggested above, so that these commands are invisible outside the needed scope.

\section{Li and He}
\newcommand{\tables}{accuracy/Li}  % path to tables
\newcommand{\ET}{Li}  % event_type shortcut


% add other sections and commands if needed

% new commands are invisible outside of this scope

But, as suggested in the comments, if you want certain commands to be visible in the entire document, you should put them into the preamble.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .