1

I have an annoying reoccurring problem. I often use user-defined macros with for example \newcommand in my LaTeX code. I also use LaTeX a lot in for example Jupyter Notebooks. Often Some things render fine locally, but don't render correctly anymore when hosted for example on Github. Or when I need to ask something on math.stackexchange, this becomes also quite annoying bringing over all macro definitions or manually expanding them.

I would therefore like to know if there is some way to produce a .tex file that has all user-defined macros expanded, so I can use the content of that document if I need cross-platform compatibility.

Example

\newcommand{\paren}[1]{\mathopen{}\left( {#1}_{{}_{}}\,\negthickspace\right)\mathclose{}}
\newcommand{\pda}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\pdb}[2]{\frac{\partial}{\partial #2} #1}
\newcommand{\grad}[2]{\nabla_{#2}{#1}}

Let the output layer activation be the sigmoid function and let the loss function be the mean square error.\\

\begin{align}
    \pda{l\paren{y, f(x)}}{A^{(L)}_i} ={}& \pda{l\paren{y, f(x)}}{f (x)_i}\\
    & \text{by definition}\\
    ={}& \pdb{\frac{1}{2n} \sum_{j=1}^n \paren{y_j - f(x)_j}^2}{f(x)}\\
    ={}& \frac{1}{2n} \sum_{j=1}^n \pda{\paren{y_j - f(x)_j^2}}{f(x)_i}\\
    ={}& \frac{1}{2n} 2 \paren{y_i - f(x)_i}\\
    ={}& \frac{1}{n} \paren{y_i - f(x)_i}\\ 
    \propto{}& y_i - f(x)_i\label{eq1}
\end{align}

\begin{equation*}
    \implies \nabla_{A^{(L)}}{l\paren{y, f(x)}} \propto y - f(x)
\end{equation*}


where $f$ is the function computed by the network and $g^{(L)}$ is the activation function of layer $L$.

This render fine locally:

enter image description here

But it renders incorrectly due to missing macro interpretation on Github:

enter image description here

  • this is a FAQ, in general the answer is no, obviously for simple cases you can use perl or python or whatever to expand known macros but latex never constructs such a thing internally so there is no way you can simply output some already expanded document – David Carlisle Oct 05 '19 at 15:09
  • I didn't think github rendered math by default at all? If you are loading mathjax then you can make the definitions available to mathjax as well. – David Carlisle Oct 05 '19 at 15:10
  • https://tex.stackexchange.com/questions/22297/is-there-a-latex-tex-preprocessor-inliner-that-replaces-only-selected-macros-wit or https://tex.stackexchange.com/questions/3174/latex-macro-expander – David Carlisle Oct 05 '19 at 15:15
  • A "mechanism" for distinguishing user defined macros from macros defined by someone else might be nice. ;-) Besides this: How to handle situations where users use \let for "copying" primitives. E.g, \let\MyRelax=\relax - \MyRelax is user-defined but not a macro. How to handle situations where user-defined macros a) do not just expand but do define other macros? b), e.g., deliver character-tokens contained in their definition-text that deviate from the usual catcode-régime? – Ulrich Diez Oct 05 '19 at 21:09
  • I assume an engine which copies/writes to file a "stream" of all those tokens that (after expansion which takes place in the "gullet") reach TeX's stomach and are used for building pages or adjusting TeX's parameters might be possible. The problem is: How to ensure at the time of processing that copy that everything gets tokenized identically to the original? – Ulrich Diez Oct 05 '19 at 21:17

0 Answers0