如何对齐包含 \verb 的多个对齐环境?

如何对齐包含 \verb 的多个对齐环境?

我需要将几个 R 语法公式对齐为方程式。

我有以下代码:

\begin{align}
&\begin{aligned}
&\verb!tActKT  ~ poly(I(1/nprocs), 3, raw=T) + poly(ndoms, 3, raw=T)!\\
&\verb!          + poly(nDOF, 3, raw=T)!\\
&\verb!          + I(1/nprocs):poly(nDOF, 2, raw=T)!
\end{aligned}\\
&\begin{aligned}
&\verb!tActDir ~ poly(I(1/nprocs), 3, raw=T) + poly(nDOF, 3, raw=T)!\\
&\verb!          + poly(ndoms, 3, raw=T)!
\end{aligned}
\end{align}

但它给了我以下错误:

LaTeX Error: \verb illegal in command argument.

你知道如何解决这个问题吗?

答案1

事实证明,你可以将其放入\verb其中\hbox

\documentclass{article}
\usepackage{mathtools}
\newsavebox{\boxA}
\newsavebox{\boxB}
\newsavebox{\boxC}
\newsavebox{\boxD}
\newsavebox{\boxE}
\begin{document}
\setbox\boxA=\hbox{\verb!tActKT  ~ poly(I(1/nprocs), 3, raw=T) + poly(ndoms, 3, raw=T)!}%
\setbox\boxB=\hbox{\verb!          + poly(nDOF, 3, raw=T)!}%
\setbox\boxC=\hbox{\verb!          + I(1/nprocs):poly(nDOF, 2, raw=T)!}%
\setbox\boxD=\hbox{\verb!tActDir ~ poly(I(1/nprocs), 3, raw=T) + poly(nDOF, 3, raw=T)!}%
\setbox\boxE=\hbox{\verb!          + poly(ndoms, 3, raw=T)!}%
% you must define the saveboxes OUTSIDE the math environment
\begin{align}
&\begin{aligned}
&\usebox\boxA \\
&\usebox\boxB \\
&\usebox\boxC
\end{aligned}\\
&\begin{aligned}
&\usebox\boxD \\
&\usebox\boxE
\end{aligned}
\end{align}
\end{document}

演示


可以使用 lstlisting 稍微简化这一点,但代价是使块中的每一行都有相同的宽度。

\documentclass{article}
\usepackage{mathtools}
\usepackage{listings}
\newsavebox{\boxA}
\newsavebox{\boxB}
\begin{document}
\setbox\boxA=\hbox{\begin{lstlisting}[basicstyle=\footnotesize\ttfamily]
tActKT  ~ poly(I(1/nprocs), 3, raw=T) + poly(ndoms, 3, raw=T) 
          + poly(nDOF, 3, raw=T)
          + I(1/nprocs):poly(nDOF, 2, raw=T)
\end{lstlisting}}
\setbox\boxB=\hbox{\begin{lstlisting}[basicstyle=\footnotesize\ttfamily]
tActDir ~ poly(I(1/nprocs), 3, raw=T) + poly(nDOF, 3, raw=T)
          + poly(ndoms, 3, raw=T)
\end{lstlisting}}
% you must define the saveboxes OUTSIDE the math environment
\begin{align}
&\usebox\boxA \\
&\usebox\boxB
\end{align}
\end{document}

列表演示

答案2

这支持相同的九条线路align(可扩展):

\documentclass{article}
\usepackage{amsmath,fancyvrb}

\newenvironment{verbalign}
  {\setcounter{verbalign}{0}}
  {\finishverbalign}

\newcounter{verbalign}
\newtoks\verbaligntoks

\newenvironment{verbline}
  {\VerbatimEnvironment\stepcounter{verbalign}%
   \begin{lrbox}{0}\begin{BVerbatim}}
  {\end{BVerbatim}\end{lrbox}%
   \global\expandafter\setbox\csname verbalignbox\theverbalign\endcsname=\box0 }

\newcommand{\finishverbalign}{%
  \verbaligntoks={%
    &\vcenter{\copy\csname verbalignbox1\endcsname}%
  }%
  \count255=1
  \loop\ifnum\count255<\value{verbalign}%
    \advance\count255 by 1
    \expandafter\addtoverbaligntoks\expandafter{\the\count255}%
  \repeat
  \begin{align}\the\verbaligntoks\end{align}
}

\newcommand{\addtoverbaligntoks}[1]{%
  \verbaligntoks=\expandafter{%
    \the\verbaligntoks\\%
    &\vcenter{\copy\csname verbalignbox#1\endcsname}%
  }%
}

\expandafter\newsavebox\csname verbalignbox1\endcsname
\expandafter\newsavebox\csname verbalignbox2\endcsname
\expandafter\newsavebox\csname verbalignbox3\endcsname
\expandafter\newsavebox\csname verbalignbox4\endcsname
\expandafter\newsavebox\csname verbalignbox5\endcsname
\expandafter\newsavebox\csname verbalignbox6\endcsname
\expandafter\newsavebox\csname verbalignbox7\endcsname
\expandafter\newsavebox\csname verbalignbox8\endcsname
\expandafter\newsavebox\csname verbalignbox9\endcsname


\begin{document}

\begin{verbalign}
\begin{verbline}
tActKT  ~ poly(I(1/nprocs), 3, raw=T) + poly(ndoms, 3, raw=T)
          + poly(nDOF, 3, raw=T)
          + I(1/nprocs):poly(nDOF, 2, raw=T)
\end{verbline}
\begin{verbline}
tActDir ~ poly(I(1/nprocs), 3, raw=T) + poly(nDOF, 3, raw=T)
          + poly(ndoms, 3, raw=T)
\end{verbline}
\end{verbalign}

\end{document}

在此处输入图片描述

相关内容