8

There is a question about minuses in listings.

Is it possible to make (document-wide) the minus sign () to look like the typographic minus, but be copied from the resulting PDF as the hyphen (-), used in source code?

1 Answer 1

10
\documentclass{article}
\usepackage{accsupp}
\newcommand\pdfminus{\BeginAccSupp{method=escape,ActualText=-}-\EndAccSupp{}}
\begin{document}
$\pdfminus A \pdfminus B = C$

$-A - B = C$
\end{document}

enter image description here

Copy/paste yields:

-A - B = C
􀀀A 􀀀 B = C

SUPPLEMENT

The OP comments on how it would be nice to have it done automatically. Unfortunately, redefining - as an active character everywhere I can only assume will break all negative numerical/length arguments. However, one could scope the \active nature of - to only apply it in places where it wouldn't break stuff:

\documentclass{article}
\let\svminus-
\usepackage{accsupp}
\newcommand\pdfminus{%
  \BeginAccSupp{method=escape,ActualText=-}\svminus\EndAccSupp{}}
{\catcode`-=\active \gdef-{\pdfminus}}
\newcommand\pdfminusON{\catcode`-=\active }
\newcommand\pdfminusOFF{\catcode`-=12 }
\begin{document}
\pdfminusON$- A - B = C$ Special $-$\pdfminusOFF

\vspace{-1cm}$-A - B = C$ Normal $-$
\end{document}

enter image description here

-A - B = C Special -
􀀀A 􀀀 B = C Normal 􀀀
5
  • Is it possible to seamlessly substitute it, without creating a new command and typing it every time? Commented Oct 31, 2018 at 13:05
  • @homocomputeris There is no way, in a general sense, to do this in pdflatex (maybe there is in lualatex...I don't know). What can be done is temporarily making - active and redirecting it to \pdfminus within the scope of the \active minus. However, you can't leave it active all the time, or it screws up any calculation requiring a length or integer subtraction. Commented Oct 31, 2018 at 13:08
  • 1
    @homocomputeris Please see the SUPPLEMENT in my answer. Commented Oct 31, 2018 at 13:21
  • Great answer, thanks! If it cannot be used globally without problems, how can I restrict it only to siunitx \num{} command? I tried defining a new command with \pdfminusON ... \pdfminusOFF but it doesn't compile. Commented Oct 31, 2018 at 13:43
  • 1
    @homocomputeris I don't know enough about siunitx to be able to fix that one, but I will keep thinking. You could always accept this answer and then ask that as a new question, citing this one. Commented Oct 31, 2018 at 14:10

You must log in to answer this question.

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