For alignment, I write a macro called \protectbox
, the core part of it is a minipage
environment. And now every time I write a displayed equation, I have to type:
\protectbox{%
\[ ... \]
}
However, sometimes I would like to typeset other .tex
file using my .cls
style, and I don't want to manually add \protectbox
to each equation. Thus I begin to think of integrate this macro into the math macro. For equation
and equation*
, it really worked:
\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
Unfortunately, this will cause an error for \[ ...\]
(the .log
file says it's an "emergency stop" without further detail).
Also, since many equations come from markdown
quick notes, formatting in $$ ... $$
, I would like to integrate \protectbox
into $$ ... $$
. But if I do so unproperly, tikz
will throw an error:
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.
Thus I'm here to ask you. I know it is kind of ugly and dangerous, but for sure, is there any way to realize this kind of integration, i.e. plug the \protectbox
macro into \[...\]
, $$...$$
, equation
, equation*
without causing serious error?
P.S. Here is the current definition of \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
I've actually asked this question in my original question, but the original one focused on line spacing, and the problem on mathematical environment was submerged in the long text, thus was not very clearly presented.
\protectbox