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)"
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}
  • just one thing and is almost done what about this character the place where linux user's live -> ~ <- I get an stranger character in linux console, when i copy it... regards., – hhlp Feb 7 at 13:31
  • @hhlp I added the ~ to the answer ;) – Phelype Oleinik Feb 7 at 13:43
  • I also have problem with this character -> 'awesome-fontconfig' <- when i copy paste is -> ’awesome-fontconfig’ <- ... regards., – hhlp Feb 8 at 14:43
  • @hhlp Done. I applied the same principle from the double quotation mark. – Phelype Oleinik Feb 8 at 14:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.