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?

4
  • 4
    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
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.

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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.