12

I cannot insert mathematics symbols inside a verbatim mode in LaTeX. This how I do:

\usepackage{alltt}

 \begin{verbatim}

 1- Train triggers model \(  X_{i}  \)  -> Y_j (X_i: source phrase
 composed of i words, Y_j : target phrase composed of j words

 \end{verbatim}

Any ideas?

Poisson
  • 339

4 Answers4

10

Just to give Bakuriu's comment as an answer and extend it a bit:

\documentclass{article}

\usepackage{alltt}

\begin{document}

\begin{alltt}
1- Train triggers model \(X\sb{i} \to Y\sb{j}\) (\(X\sb{i}\): source phrase
composed of \(i\) words; \(Y\sb{j}\): target phrase composed of \(j\) words)
\end{alltt}
or
\begin{alltt}
1- Train triggers model \(X\sb{i}\,\to\,Y\sb{j}\) (\(X\sb{i}\): source phrase
composed of \(i\) words; \(Y\sb{j}\): target phrase composed of \(j\) words)
\end{alltt}
or
\begin{alltt}
1- Train triggers model \(X\sb{i}\to{Y\sb{j}}\) (\(X\sb{i}\): source phrase
composed of \(i\) words; \(Y\sb{j}\): target phrase composed of \(j\) words)
\end{alltt}

\end{document}

output

Note the differences in spacing around \to in the three approaches.

Note: See http://www.tug.org/texlive/Contents/live/texmf-dist/doc/latex/base/alltt.pdf for the implementation of the package and a few 'HowTo's.

8

A variant with package listings:

\documentclass{article}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[
  mathescape,
  columns=fullflexible,
  basicstyle=\fontfamily{lmvtt}\selectfont,
]
1- Train triggers model $ X_{i} \to Y_j$ ($X_i$: source phrase
composed of $i$ words, $Y_j$: target phrase composed of $j$ words)
\end{lstlisting}
\end{document}

Result

Remarks:

  • listings detects inline math ($...$), if option mathescape is given. \(...\) is not detected here.
  • There are no alignment issues to be seen, thus columns=fullflexible looks much nicer.
  • Also the font is the variable width typewriter font from the Latin Modern fonts (more modern variant of Computer Modern).
Heiko Oberdiek
  • 271,626
3

With fvextra that allows mathescape and the help of https://tex.stackexchange.com/a/385068/4427, we can also set the math symbols in typewriter type.

\documentclass{article}
\usepackage{fvextra} % loads also fancyvrb
\usepackage{xpatch}

\DeclareMathVersion{ttmath}
\DeclareSymbolFont{latinletters}{OT1}{\ttdefault}{m}{n}
%\SetSymbolFont{latinletters}{ttmath}{OT1}{\ttdefault}{m}{n}
\SetSymbolFont{letters}{ttmath}{OML}{ccm}{m}{it}
\SetSymbolFont{symbols}{ttmath}{OMS}{ccsy}{m}{n}
\SetSymbolFont{largesymbols}{ttmath}{OMX}{ccex}{m}{n}

\newcommand{\changeletters}{%
  \count255=`A
  \advance\count255 -1
  \loop\ifnum\count255<`Z
    \advance\count255 1
    \mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
  \repeat
  \count255=`a
  \advance\count255 -1
  \loop\ifnum\count255<`z
    \advance\count255 1
    \mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
  \repeat
  \count255=`0
  \advance\count255 -1
  \loop\ifnum\count255<`9
    \advance\count255 1
    \mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
  \repeat
}

\xapptocmd{\ttfamily}{\mathversion{ttmath}\changeletters}{}{}


\begin{document}

\begin{Verbatim}[mathescape,commandchars=\\\{\}]
1 - Train triggers model $X_i \to Y_j$ ($X_i$: source phrase
composed of $i$ words, $Y_j$: target phrase composed of $j$ words
\end{Verbatim}

\end{document}

enter image description here

If you prefer the math letters to be in italics, just change the line after \DeclareMathVersion{ttmath} into

\DeclareSymbolFont{latinletters}{OT1}{\ttdefault}{m}{n}

enter image description here

Also alltt can be used (here I show italic symbols)

\documentclass{article}
\usepackage{alltt}
\usepackage{xpatch}

\DeclareMathVersion{ttmath}
\DeclareSymbolFont{latinletters}{OT1}{\ttdefault}{m}{it}
%\SetSymbolFont{latinletters}{ttmath}{OT1}{\ttdefault}{m}{n}
\SetSymbolFont{letters}{ttmath}{OML}{ccm}{m}{it}
\SetSymbolFont{symbols}{ttmath}{OMS}{ccsy}{m}{n}
\SetSymbolFont{largesymbols}{ttmath}{OMX}{ccex}{m}{n}

\newcommand{\changeletters}{%
  \count255=`A
  \advance\count255 -1
  \loop\ifnum\count255<`Z
    \advance\count255 1
    \mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
  \repeat
  \count255=`a
  \advance\count255 -1
  \loop\ifnum\count255<`z
    \advance\count255 1
    \mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
  \repeat
  \count255=`0
  \advance\count255 -1
  \loop\ifnum\count255<`9
    \advance\count255 1
    \mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
  \repeat
}

\xapptocmd{\ttfamily}{\mathversion{ttmath}\changeletters}{}{}


\begin{document}

\begin{alltt}
1 - Train triggers model \(X\sb{i}\to{}Y\sb{j}\) (\(X\sb{i}\): source phrase
composed of \(i\) words, \(Y\sb{j}\): target phrase composed of \(j\) words
\end{alltt}

\end{document}

The output is the same as the second picture above; however, this has disadvantages as the math input needs to be very careful so as not to introduce unwanted spaces.

egreg
  • 1,121,712
0

The answer that fixed this for me is actually buried in Bakuriu's comment on the question. I quote their comment..

You should use \begin{alltt} not \begin{verbatim}. The alltt package does not modify the verbatim environment. It provides a new environment that acts like verbatim but that allows to use maths mode etc. Read the documentation for alltt for other information.

Only posting here so that it is easier to find as many go right to the answers.

Tommy
  • 101
  • 2