# How to automatically generate code

I am writing a paper that has lots of equations and it gets tedious having to retype my layout for each equation. Is there a way to automatically generate my next few lines of code after I type

For example, currently I have this:

\documentclass [a4paper,12pt]{report}
\usepackage{amsmath}
\usepackage{array,tabularx,calc}
\newlength{\conditionwd}
\newenvironment{conditions}[1][where:]
{%
#1\tabularx{\textwidth-\widthof{#1}}[t]{
>{$}l<{$} @{${}={}$} X@{}%>{\raggedright\arraybackslash}X@{}
}%
}

\begin{document}
A computational model was devloped by \citet{brown1977glue} which performs the necessary calculations for transformed section analysis. The program calculates the maximum moment (m) using equation \ref{eqn:PROLAM moment}

$$m_i = \frac{E_s}{E_j} \left( \frac{I_sF_i}{C_i} \right) \label{eqn:PROLAM moment}$$
where:
\begin{itemize}[labelindent=20pt,leftmargin=*,widest=$M_x$,align=left,itemsep=0pt]
\item[$E_s$]        is the MOE for the standard material
\item[$E_j$]        MOE for the $i$-th lamination
\item[$F_i$]        Allowable stress of the $i$-th lamination
\item[$I_s$]        Is the moment of inertia of the transformed section
\item[$C_i$]        "Distance between the neutral axis of the transformed section and the extreme fiber of the $i$-th lamination"
\end{itemize}

\end{document}


Which produces a layout like this:

Is it possible for me type out something like \begin{equationlist} or \equationlist and have the following code automatically generated:

$$\label{eqn:MY_LABEL}$$
where:
\begin{itemize}[labelindent=20pt,leftmargin=*,widest=$M_x$,align=left,itemsep=0pt]
\item[]
.
.
.
\end{itemize}


I'm unsure if this has been asked anywhere else, i'm not exactly sure what to search for. I could just scroll up in my code and copy paste but generating this automatically would save me loads of time.

Edit: I am using Overleaf as my Tex editor

• I think this is editor-specific; look into whether yours supports so-called "snippets" – steve May 22 '20 at 15:47
• To save you from repeating \begin{itemize}[labelindent=20pt,leftmargin=*,widest=$M_x$,align=left,itemsep=0pt] over and over again, you could define your own list-like environment that automatically applies these options. If you decide to change the spacing later on, you only need to change it in the preamble once and it is applied throughout the document. – leandriis May 22 '20 at 16:08
• @leandriis each equation has a different widest= This equation's just happened to be $M_x$. so this would be a parameter that changed everytime. Is it possible to set up this list-like environment with a changeable parameter? If so, any idea how I would go about it? – Aaron Rhodes May 22 '20 at 17:01
• You could globally define everything that will not change, while still using widets=... locally: \documentclass{article} \usepackage{enumitem} \newlist{myconditions}{itemize}{1} \setlist[myconditions]{labelindent=20pt,leftmargin=*,align=left,itemsep=0pt} \begin{document} \begin{myconditions}[widest=$M_x$] \item[$E_s$] is the MOE for the standard material \end{myconditions} \begin{myconditions}[widest=long item label] \item[long item label] is the MOE for the standard material \end{myconditions} \end{document} – leandriis May 22 '20 at 17:06
• For some reason I must be doing something wrong, I still cant get it working – Aaron Rhodes May 22 '20 at 17:30

Depending what you want to get automatized, you could create a new command. I would suggest something like:

\newcommand\equationlist[2]{        % Params are the equation and the label
$$#1 \label{eqn:#2}$$
where:
\begin{itemize}[labelindent=20pt,leftmargin=*,widest=$M_x$,align=left,itemsep=0pt]
\item[]
\end{itemize}
}


Then usage is simply:

\equationlist{m_i = \frac{E_s}{E_j} \left( \frac{I_sF_i}{C_i} \right)}{PROLAM moment}


As suggested by Steve, you may want to have a look at editor-specific snippets for more automation.

Now, following comments, here is a version that allows 'where' items to be given as parameters. It requires at least one parameter. Some code may be added to check whether one parameter has been given or not. I don't think this thing is useful as there is no economy in writing this. But, at least, it admits a variable number of parameters, thanks to this page.

Full code :

\documentclass [a4paper,12pt]{report}
\usepackage{amsmath}
\usepackage{enumitem}
\usepackage{array,tabularx,calc}

\newlength{\conditionwd}
\newenvironment{conditions}[1][where:]
{%
#1\tabularx{\textwidth-\widthof{#1}}[t]{
>{$}l<{$} @{${}={}$} X@{}%>{\raggedright\arraybackslash}X@{}
}%
}

\newcommand\equationlist[4]{        % Params are the equation, the label, the widest and the 'where' items
$$#1 \label{eqn:#2}$$
\whereitems{#3}{#4}
}

\newcommand\whereitem[2]{\item[#1] #2}

\makeatletter
\newcommand{\checknextarg}{\@ifnextchar\bgroup{\gobblenextarg}{\end{itemize}}}
\newcommand{\gobblenextarg}[1]{ \item #1\@ifnextchar\bgroup{\gobblenextarg}{\end{itemize}}}
\makeatother

\newcommand{\whereitems}[2]{%   Params are widest and at least one parameter
where:%
\begin{itemize}[labelindent=20pt,leftmargin=*,widest=#1,align=left,itemsep=0pt]
#2
\checknextarg
}

\begin{document}
A computational model was devloped by [\ldots] which performs the necessary calculations for transformed section analysis. The program calculates the maximum moment (m) using equation \ref{eqn:PROLAM moment}

\equationlist{
m_i = \frac{E_s}{E_j} \left( \frac{I_sF_i}{C_i} \right)
}{PROLAM moment}{$M_x$}{%
\whereitem{$E_s$}{is the MOE for the standard material}
\whereitem{$E_j$}{MOE for the $i$-th lamination}
\whereitem{$F_i$}{Allowable stress of the $i$-th lamination}
\whereitem{$I_s$}{Is the moment of inertia of the transformed section}
\whereitem{$C_i$}{"Distance between the neutral axis of the transformed section and the extreme fiber of the $i$-th lamination"}
}

\end{document}

• How will the itemize be filled with items? – leandriis May 22 '20 at 16:14
• It was unclear to me this was needed by the requester as he wrote brackets and not dots or something to complete. But of course if it is needed this is only a partial solution and I'll delete it as an answer. – Ivan May 22 '20 at 16:19
• There just needs to be somewhere where i would be able to define all the itemize parts. I'm not sure they could be define as a parameters as each equation has a different number of things to itemize. I have however edited my question to include the dots. – Aaron Rhodes May 22 '20 at 16:28
• I edited the above proposed answer. Now you can pass your 'where' items as parameters (minimum one, as many as you want) and the 'widest' is also a param. – Ivan May 22 '20 at 17:37