如何自动生成代码

如何自动生成代码

我正在写一篇包含大量方程式的论文,必须为每个方程式重新输入布局,这很乏味。有没有办法在我输入后自动生成接下来的几行代码\begin{equation}

例如,目前我有这个:

\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@{}
   }%
  }
  {\endtabularx\par\addvspace{\belowdisplayskip}}


\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}

    \begin{equation}
        m_i = \frac{E_s}{E_j} \left( \frac{I_sF_i}{C_i} \right)
     \label{eqn:PROLAM moment}
\end{equation}
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}

产生如下布局:

在此处输入图片描述

我是否可以输入类似的内容\begin{equationlist}\equationlist并自动生成以下代码:

\begin{equation}

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

我不确定其他地方是否问过这个问题,我不太清楚要搜索什么。我可以直接在代码中向上滚动并复制粘贴,但自动生成代码可以节省我大量时间。

编辑:我使用 Overleaf 作为我的 Tex 编辑器

答案1

根据您想要实现的自动化程度,您可以创建一个新命令。我建议这样做:

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

然后用法很简单:

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

正如史蒂夫所建议的,您可能需要查看编辑器特定的片段以实现更多自动化。

现在,根据评论,这里有一个版本,允许将“where”项作为参数给出。它至少需要一个参数。可以添加一些代码来检查是否给出了一个参数。我不认为这个东西有用,因为写这个不经济。但至少,它允许可变数量的参数,这要归功于这一页

完整代码:

\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@{}
    }%
}
{\endtabularx\par\addvspace{\belowdisplayskip}}


\newcommand\equationlist[4]{        % Params are the equation, the label, the widest and the 'where' items
    \begin{equation}
        #1  
        \label{eqn:#2}
    \end{equation}
    \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}

相关内容