# Insert symbols inside verbatim mode LaTeX

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?

• 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. – Bakuriu Dec 22 '13 at 11:57
• Or define an escape char. – Ulle Dec 22 '13 at 12:39
• What is the reason for verbatim? It does not look like a typical example for verbatim text. – Heiko Oberdiek Jan 29 '14 at 7:04
• Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. – samcarter_is_at_topanswers.xyz Oct 23 '16 at 19:27

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}


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.

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}


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).

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{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
\loop\ifnum\count255<Z
\mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
\repeat
\count255=a
\loop\ifnum\count255<z
\mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
\repeat
\count255=0
\loop\ifnum\count255<9
\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}


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

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


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
\loop\ifnum\count255<Z
\mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
\repeat
\count255=a
\loop\ifnum\count255<z
\mathcode\count255=\numexpr\number\symlatinletters*256+\count255\relax
\repeat
\count255=0
\loop\ifnum\count255<9
\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.

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.