我正在使用列表来显示 bash 脚本,例如
$ export MY_ENV=`pwd`
我怎样才能使列表显示正确的字符 `. 现在它显示 '
梅威瑟:
\documentclass[11pt, article, oneside, a4paper]{memoir}
% Files encoding
\usepackage[utf8]{inputenc}
\usepackage[spanish, es-noquoting, es-tabla]{babel}
%es-noquoting era por el tikz
\usepackage[spanish]{translator}
\usepackage[OT1]{fontenc}
%\usepackage[T1]{fontenc}
%Listings
\usepackage[final]{listings} % to be shown independently draft of final
\lstdefinestyle{CommandLineStyle}{
basicstyle=\small\ttfamily,
numbers=none,
% frame=tblr,
columns=fullflexible,
% backgroundcolor=\color{blue!10},
linewidth=0.95\linewidth,
xleftmargin=0.05\linewidth,
breaklines=true,
breakindent=1em,
% inputencoding=utf8/latin1,
% postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}},
% breakautoindent=true
}
\begin{document}
\begin{lstlisting}[style=CommandLineStyle]
$ export MY_ENV=`pwd`
\end{lstlisting}
\end{document}
答案1
该listings
包提供了upquote
切换选项来确定如何打印左引号和右引号。将其设置为true
可实现您想要的效果(与设置 egreg 的答案中的 literate 类似)。这需要textcomp
在设置选项之前加载包。
\documentclass[article]{memoir}
\usepackage{textcomp} % <--- for other glyphs, so can use upquote option of listings
\usepackage[final]{listings} % to be shown independently draft of final
\lstdefinestyle{CommandLineStyle}{
basicstyle=\small\ttfamily,
numbers=none,
upquote=true% ensure that backtick displays correctly
}
\begin{document}
\begin{lstlisting}[style=CommandLineStyle]
$ export MY_ENV=`pwd`
\end{lstlisting}
\end{document}
答案2
您可以使用literate
的功能listings
和\textasciigrave
的宏textcomp
。我已将示例精简至最基本部分。
\documentclass[article]{memoir}
\usepackage{textcomp} % <--- for other glyphs
\usepackage[final]{listings} % to be shown independently draft of final
\lstdefinestyle{CommandLineStyle}{
basicstyle=\small\ttfamily,
numbers=none,
% frame=tblr,
columns=fullflexible,
% backgroundcolor=\color{blue!10},
linewidth=0.95\linewidth,
xleftmargin=0.05\linewidth,
breaklines=true,
breakindent=1em,
% inputencoding=utf8/latin1,
% postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}},
% breakautoindent=true,
literate={`}{\textasciigrave}{1}, <--- real back quote
}
\begin{document}
\begin{lstlisting}[style=CommandLineStyle]
$ export MY_ENV=`pwd`
\end{lstlisting}
但如果你使用 Bash,为什么不
export MY_ENV=$(pwd)
哪个打印起来更简单?;-)