我正在编写一个通用文档,以便任何人都可以遵循它,其想法是将此命令复制并粘贴到 Linux 终端中。但是当我在终端中执行此操作时,会出现错误,因为引号显示为这样”
而不是这样"
,连字符显示为短划线−
(大划线)而不是这样-
。
我的成果:
\lstdefinestyle{BashInputStyle}{
language=bash,
basicstyle=\small\sffamily,
numbers=left,
numberstyle=\tiny,
numbersep=3pt,
frame=tb,
columns=fullflexible,
backgroundcolor=\color{yellow!20},
linewidth=0.9\linewidth,
xleftmargin=0.1\linewidth
}
例如这个:
\begin{lstlisting}[style=BashInputStyle]
$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
\end{lstlisting}
生成此 PDF 输出:
从 PDF 复制的文本是:
sh −c ”$(curl −fsSL https://raw.github.com/robbyrussell/oh−my−zsh/master/tools/install.sh)”
但它应该是这样的:
sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
答案1
使用literate
键,您可以更改字符的定义。为了制作直立的引号,我literate
使用"
和'
符号替换了\textquotedbl
和\textquotesingle
,来自textcomp
包(您必须使用T1
的编码\textquotedbl
)。对于 ,-
将其添加到列表中就足够了。
波浪符号有点棘手。要使复制粘贴正确,你只需加载textcomp
并使用\texttildelow
,但这样看起来很奇怪。我在这个答案,通过将字体局部更改为ptm
(Times)。
PDF 输出:
以及从 PDF 复制的文本:
$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
cd '~/test/awesome-fontconfig'
梅威瑟:
\documentclass{article}
\usepackage{textcomp}
\usepackage[margin=1cm]{geometry}
\usepackage{xcolor}
\usepackage{listings}
\newcommand\uprightquote{{\fontencoding{T1}\selectfont\textquotedbl}}
\newcommand\centeredtilde{{\fontfamily{ptm}\selectfont\texttildelow}}% From this answer: https://tex.stackexchange.com/a/160898/134574
\lstdefinestyle{BashInputStyle}{
language=bash,
basicstyle=\small\sffamily,
numbers=left,
numberstyle=\tiny,
numbersep=3pt,
frame=tb,
columns=fullflexible,
backgroundcolor=\color{yellow!20},
linewidth=0.9\linewidth,
xleftmargin=0.1\linewidth,
literate =
{"}{{\uprightquote}}1
{'}{{\textquotesingle}}1
{-}{{-}}1
{~}{{\centeredtilde}}1
,
}
\begin{document}
\begin{lstlisting}[style=BashInputStyle]
$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
cd '~/test/awesome-fontconfig'
\end{lstlisting}
\end{document}