2

I'm writing a document for general purpose so that anyone can follow it and the idea is to copy and paste this command in a Linux terminal. But when I do that in a terminal give me error because quotation marks are displayed like this instead of " and hyphens are rendered as an en-dash (a large dash) instead of this -.

What I achieved:

\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
}

For example this:

\begin{lstlisting}[style=BashInputStyle]
$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
\end{lstlisting}

produces this PDF output:

enter image description here

and the text copied from the PDF is:

sh −c ”$(curl −fsSL https://raw.github.com/robbyrussell/oh−my−zsh/master/tools/install.sh)”

but it should be like this:

sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
hhlp
  • 23

1 Answers1

2

Using the literate key you can change the definition of the characters. To make the upright quotation marks I literated the " and ' symbols to be replaced by \textquotedbl and \textquotesingle, from the textcomp package (you have to use the T1 encoding for \textquotedbl). For the - it was enough to add it to the list.

The tilde is a little bit trickier. To make the copy-pasting correct you just need to load textcomp and use \texttildelow, but then it looks weird. I found a hint in this answer, by locally changing the font to ptm (Times).

The PDF output:

enter image description here

and the text copied from the PDF:

$ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
cd '~/test/awesome-fontconfig'

MWE:

\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}