0

I need to align several R-syntax formulas as equations.

I have a following code:

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

But it's giving me following error:

LaTeX Error: \verb illegal in command argument.

Do you know, how to fix this problem?

Eenoku
  • 523
  • Generally \verb cannot be used inside other macros. The way align/aligned gathers up its contents probably does not allow the use of \verb in this context. What exactly are you trying to do here? Why should these be numbered as math? – daleif May 02 '17 at 13:49
  • @daleif Because I'm listing many pretty large models, which can be written down using R-syntax, like here. Without it, those equations would be extremely long and hard to read. – Eenoku May 02 '17 at 13:52
  • "Normal" LaTeX verbatim-material is not permitted inside other macros. However, this restriction can be eased if you're willing to switch to LuaLaTeX. Have you taken a look at the posting How to handle verbatim material in LuaLaTeX? One of the answers describes how to use LuaTeX's process_input_buffer callback to read in and play back verbatim material. – Mico May 02 '17 at 14:08
  • 1
    I would still never write stuff like this in math mode, but rather as numbered listings using the listings package. – daleif May 02 '17 at 14:21

2 Answers2

1

As it turns out, you can put \verb inside a \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}

demo


One can simplify this a bit using lstlisting, but at the cost of making every line in a block the same width.

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

listings demo

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
1

This supports nine lines in the same align (extendable):

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

enter image description here

egreg
  • 1,121,712