6

I'd like to have code in a PDF that someone can simply copy, paste, and use. Currently, \lstlisting is converting '-' to em dashes. Then when I paste the output into the Python interpreter or MATLAB, I get syntax errors. None of the solutions I've looked at on here seem to work.

myvar = 9
myvar = myvar - 2

The '-' ends up as '−' in the output.

I have the following settings

\lstset{breakatwhitespace,
language=MATLAB,
columns=fullflexible,
keepspaces,
breaklines,
tabsize=3, 
showstringspaces=false,
extendedchars=true,
basicstyle=\fontfamily{pcr}\selectfont\scriptsize,
keywordstyle=\color{orange},
upquote=true}

I am using \documentclass{beamer}

I've looked at

  1. listings package changes hyphens to minus signs
  2. less relevant since i'm focusing on code, not comments: Avoiding "minus sign" dash in listings commentstyle
2

1 Answer 1

7

You can use literate to change possible automated conversions. In this specific case, using literate={-}{-}1 substitutes - for - as-is:

\documentclass{beamer}

\usepackage{listings,textcomp}

\lstset{
  breakatwhitespace,
  language=MATLAB,
  columns=fullflexible,
  keepspaces,
  breaklines,
  tabsize=3, 
  showstringspaces=false,
  extendedchars=true,
  basicstyle=\fontfamily{pcr}\selectfont\scriptsize,
  keywordstyle=\color{orange},
  upquote=true,
  literate={-}{-}1}

\begin{document}

\begin{frame}[fragile]
\frametitle{A listing}

\begin{lstlisting}
myvar = 9
myvar = myvar - 2
\end{lstlisting}

\end{frame}

\end{document}
1
  • Thank you. This is very nice and simple. I was fearful of the other solutions that used \makeatletter and so on, things I'm not too familiar with. The issue with having to respecify fonts seems to have fixed itself as well.
    – bernie
    Commented Sep 30, 2016 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .