我无法在 LaTeX 的逐字模式中插入数学符号。我这样做:
\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}
有任何想法吗?
答案1
仅将 Bakuriu 的评论作为答案并稍加扩展:
\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}
\to
请注意三种方法中间距的差异。
注意:请参阅http://www.tug.org/texlive/Contents/live/texmf-dist/doc/latex/base/alltt.pdf用于实施该软件包以及一些“HowTo”。
答案2
带有包装的变体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}
评论:
listings
如果给出了选项,则检测内联数学($
...$
)。...这里未检测到。mathescape
\(
\)
- 看不到任何对齐问题,因此
columns=fullflexible
看起来更加美观。 - 该字体也是拉丁现代字体(计算机现代字体的更现代变体)的可变宽度打字机字体。
答案3
有了fvextra
这些允许mathescape
和帮助https://tex.stackexchange.com/a/385068/4427,我们还可以用打字机类型设置数学符号。
\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}
如果你希望数学字母采用斜体,只需将后面的行更改\DeclareMathVersion{ttmath}
为
\DeclareSymbolFont{latinletters}{OT1}{\ttdefault}{m}{n}
也alltt
可以使用(这里我用斜体符号表示)
\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}
输出与上面的第二张图片相同;但是,这有缺点,因为数学输入需要非常小心,以免引入不需要的空格。
答案4
为我解决这个问题的答案实际上隐藏在 Bakuriu 对这个问题的评论中。我引用他们的评论。
您应该使用 \begin{alltt} 而不是 \begin{verbatim}。alltt 包不会修改 verbatim 环境。它提供了一个新的环境,其作用类似于 verbatim,但允许使用数学模式等。请阅读 alltt 的文档以了解其他信息。
仅在这里发布,以便更容易找到更多直接找到答案。