How to abstract formulas (or graphic objects) in LaTeX

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 4 '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 4 '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 tex.stackexchange.com/questions/119898/… or tex.stackexchange.com/questions/229313/… – Michael Palmer Jul 4 '17 at 19:57

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{%
$$3^2+4^2=5^2$$}


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

\usepackage{mycommands}
...
\cube