将一些宏集成到数学环境中

将一些宏集成到数学环境中

为了对齐,我编写了一个名为的宏\protectbox,它的核心部分是一个minipage环境。现在每次我写一个显示方程时,我都必须输入:

\protectbox{%
    \[ ... \]
}

但是,有时我想.tex用我的风格排版其他文件.cls,并且不想手动添加\protectbox到每个等式中。因此我开始考虑将这个宏集成到数学宏中。对于equationequation*,它确实有效:

\RequirePackage{amsmath}
\RequirePackage{environ}
\let\MYequation\equation
\let\endMYequation\endequation
\RenewEnviron{equation}{%
\protectbox[-1]{%
\begin{MYequation}%
    \BODY%
\end{MYequation}}%
}
\makeatletter
\RenewEnviron{equation*}{%
\protectbox[-1]{%
\begin{MYequation}%
    \st@rredtrue \global\@eqnswfalse% this is copied from amsmath.sty
    \BODY%
\end{MYequation}}%
}
\makeatother

不幸的是,这将导致错误\[ ...\].log文件说这是一个“紧急停止”,但没有进一步的细节)。

另外,由于许多方程式来自markdown快速笔记,在 中格式化$$ ... $$,我希望将其整合\protectbox到 中$$ ... $$。但如果我做得不正确,tikz就会引发错误:

Package tikz: Sorry, some package has redefined the meaning of the math-mode dollar sign. This is incompatible with tikz and its calc library and might cause unrecoverable errors.

因此我来这里问你。我知道这有点丑陋和危险,但可以肯定的是,有什么办法可以实现这种整合,即将\protectbox宏插入\[...\]$$...$$equationequation*不会导致严重错误?


PS 以下是的当前定义\protectbox

\RequirePackage{calc}
\makeatletter
\newlength{\@temp@length}
\newcommand{\protectbox}[2][0]{% the first parameter is an offset of length, the second is the content
    % measure the height of contents
    \setbox0=\vbox{%
        \abovedisplayshortskip=0pt%
        \belowdisplayshortskip=0pt%
        \abovedisplayskip=0pt%
        \belowdisplayskip=0pt%
        #2}%
    \@temp@length=\ht0 \advance\@temp@length by \dp0%
    % calculate the number of lines needed to place the content
    \setlength{\@temp@length}{\numexpr\@temp@length/1864679\relax cm + #1\baselineskip}%
    \setlength{\protectboxskip}{0.5\@temp@length}%
    \vspace{\protectboxskip}%
    \vspace{-0.3\baselineskip}%
    \par\noindent%
    \begin{minipage}[c][\baselineskip]{\linewidth}%
        \abovedisplayshortskip=0pt%
        \belowdisplayshortskip=0pt%
        \abovedisplayskip=0pt%
        \belowdisplayskip=0pt%
        #2%
    \end{minipage}%
    \vspace{\protectboxskip}%
    \vspace{0.3\baselineskip}%
}
\makeatother

我实际上已经问过这个问题我原来的问题但原文的重点在于行距,而数学环境的问题被淹没在长文中,呈现得不是很清楚。

答案1

好吧,现在我可以回答我自己的问题了。

回想一下,这道题的意思是实现如下的便条纸效果: 在此处输入图片描述


它的工作原理如下。

首先定义一个,\protectbox计算内容的高度(或者手动给定高度),并使其成为的整数倍\baselineskip,使得后面的文本保持在正确的线上。

% \protectbox<optional, space ajust>{optional, height}{content}
\newlength{\protectboxskip}
\NewDocumentCommand\protectbox{D<>{0}om}
{
  \IfNoValueTF{#2}%
  {% Height not given
    % measure the height
    \setbox0=\vbox{%
    \abovedisplayshortskip=0pt%
    \belowdisplayshortskip=0pt%
    \abovedisplayskip=0pt%
    \belowdisplayskip=0pt%
    #3}%
    \@temp@length=\ht0 \advance\@temp@length by \dp0%
    % assign the height
    \setlength{\@temp@length}{\numexpr\@temp@length/\baselineskip\relax\baselineskip + #1\baselineskip}%
  }%
  {% Height given
    \setlength{\@temp@length}{#2\baselineskip-\baselineskip}%
  }
  \setlength{\protectboxskip}{0.5\@temp@length}%
  % space above
  \vspace{\parskip}%
  \vspace{\baselineskip}%
  \vspace*{\protectboxskip}%
  \vspace*{-0.2\baselineskip}%
  \vspace{-\parskip}%
  \vspace{-\baselineskip}%
  \par%
  \noindent%
  % the content
  \begin{minipage}[c][\baselineskip]{\linewidth}%
  \abovedisplayshortskip=0pt%
  \belowdisplayshortskip=0pt%
  \abovedisplayskip=0pt%
  \belowdisplayskip=0pt%
  #3%
  \end{minipage}%
  % space after
  \vspace{\protectboxskip}%
  \vspace{0.2\baselineskip}%
}

下一步是将此宏集成到其他命令或环境中,例如,,equation...从开始tablefigureequation

% Redefine equation and equation*
\let\equation\equation
\let\endequation\endequation
\RenewEnviron{equation}{%
\addtocounter{equation}{-1}
\protectbox<0>{%
\begin{equation}%
  \BODY%
\end{equation}}%
\par\noindent%
}
\RenewEnviron{equation*}{%
\protectbox<0>{%
\addtocounter{equation}{-1}
\begin{equation}%
  \st@rredtrue \global\@eqnswfalse%
  \BODY%
\end{equation}}%
\par\noindent%
}

然后\[...\]

% Redefine \[...\]
\def\[#1\]{\begin{equation*}#1\end{equation*}}

并且还有tablefigure为了对齐,我们必须使其不浮动):

% Redefine table
% \setlength{\intextsep}{0cm}
\let\FCtable\table
\let\endFCtable\endtable
\RenewEnviron{table}[1][]{%
\addtocounter{table}{-1}%
\FCprotectbox{%
  \def\@captype{table}
  \BODY%
}%
\par\noindent%
}

% Redefine figure
\let\FCfigure\figure
\let\endFCfigure\endfigure
\RenewEnviron{figure}[1][]{%
\addtocounter{figure}{-1}%
\FCprotectbox{%
  \def\@captype{figure}
  \BODY%
}%
\par\noindent%
}

最后,可以重新定义$$,但这样做并不安全,因为使用tikz及其calc库时可能会导致错误。我个人定义了一个选项safemode,并将此重新定义保留在下面:

\if@safemode\endinput\fi % This have to be pre-defined

% Redefine $$
\global\let\tikz@ensure@dollar@catcode=\relax
\catcode`\$=\active
\protected\def${\@ifnextchar$\@doubledollar\@singledollar}
\def\@doubledollar$#1$${\begin{equation*}#1\end{equation*}}
\def\@singledollar#1${\(#1\)}

相关内容