[Tex/LaTex] backward quote in listings

bashsymbols

I'm typesetting a document with examples of bash code. How do I make the code like

`pwd -P`

appear with the same kind of quotes? I want the readers of the document to be able to copy and paste the code directly into a terminal. Latex is giving me the opening single quote symbol ‘.

Best Answer

You could also use the literate key to define literate character replacements:

\documentclass{article}
\usepackage{listings}

\def\backtick{\char18}
\lstdefinestyle{mystyle}{literate={`}{\backtick}1, escapechar=@}

\begin{document}
\begin{lstlisting}[style=mystyle]
  @\backtick@pwd -P@\backtick@
  `pwd -P`
\end{lstlisting}
\end{document}

which gives

enter image description here

The copy/paste result from the PDF file is the same for both lines for me. However, note the small spacing difference. I'm not entirely sure which of these spacings is the correct one, but I'm temped to say it's the one of the second line because the literate explicitly tells listings to treat the replacement as a single character.