1

Assume you have many documents in LaTeX (for example, Math word problems) and don't want to write the same formulas once and again. Instead, you would prefer to write something like \pythagoreantheorem and expect that this term is replaced by the right expression when rendering, expression that you have defined somewhere else.
Does LaTeX provide with such a mechanism to define system formulas that can be invoked from any document?

(Same question for graphics: any way to define system graphic objects that can be invoked in a document? - you write \cube and it gets replaced by the corresponding TikZ code).

  • yes tex is a macro processor: what you describe is the basis of the entire language. – David Carlisle Jul 04 '17 at 19:53
  • But, where do you define such macros?, where is to be stored the formula and the label associated to it? (or the graphic object?) – nightcod3r Jul 04 '17 at 19:56
  • Are you familiar with \newcommand? that is the standard way to define custom macros in LateX. If you want to reuse the same macro in different documents, you can collect them in some personal package of yours. See for example https://tex.stackexchange.com/questions/119898/latex-cannot-find-my-personal-packages or https://tex.stackexchange.com/questions/229313/windows-tex-live-and-directory-for-personal-packages – Michael Palmer Jul 04 '17 at 19:57

1 Answers1

5

The entire tex language is based on macro expansion replacing commands by their definitions as you describe.

Just make a file, say mycommands.sty that has

\RequirePackage{tikz}

\newcommand\cube{\begin{tikzpicture}... whatever \end{tikzpicture}}

\newcommand\pythagoreantheorem{%
\begin{equation}3^2+4^2=5^2\end{equation}}

place that in your default tex input path then in any document you can use

\usepackage{mycommands}
...
\cube
David Carlisle
  • 757,742