# executable code containing symbol ^ inserted with verbatim on pdf. Error while copying back into a text editor

I'm trying to insert Octave code in a pdf containing the character ^. However when I copy the code from the generated pdf, the code doesn't run: The I already posted similar question and could solve the problem. However the problem reappeared. I suspect one of the loaded package is responsible.

Here is an example of code with the loaded packages

\documentclass[a4paper, 12pt]{book}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{url}
\usepackage{framed}
\usepackage{setspace}
\usepackage{lmodern}
\usepackage{multirow}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{upquote}
\usepackage{hyperref}
\hypersetup{
filecolor=magenta,
urlcolor=cyan,
}

\usepackage{caption}
%\usepackage{calrsfs}
%\DeclareMathAlphabet{\pazocal}{OMS}{zplm}{m}{n}

\begin{document}
\setlength{\parindent}{0cm}
\renewcommand{\baselinestretch}{0.5}
\title{Notes}
\author{John Smith}
\date{\today}
\maketitle

\tableofcontents{}

\begin{verbatim}
x^2
\end{verbatim}

\end{document}

• Code produced by a listing is not necessarily meant to run and one shouldn't expect it to after just copy&pasting it. – Skillmon Dec 2 '18 at 21:55
• indeed when copying pasting from PDF I get the MODIFIER LETTER CIRCUMFLEX ACCENT U+02C6 (letter ˆ) and not the usual ascii caret. Culprit is lmodern package. But add \usepackage[T1]{fontenc} and it should fix the issue. – user4686 Dec 3 '18 at 21:58
• MWE: simply load lmodern and consider \texttt{x\string^2}. – user4686 Dec 3 '18 at 22:03
• @UlrikeFischer you may be interested into this why does \OT1/lmtt/m/n/10 ^ give a ˆ in PDF (or at least when copying back from PDF, tested with Skim on mac os x) – user4686 Dec 3 '18 at 22:04

I am providing this as a provisory answer to give a workaround awaiting the fonts expert (... or experts).

MWE:

\documentclass{article}
%\usepackage[T1]{fontenc}
\usepackage{lmodern}
\begin{document}
\thispagestyle{empty}
\texttt{x\string^2}
\showoutput
\end{document}


Uncomment the fontenc line to fix the issue. Then copy-pasting from PDF gives a normal ascii caret, not a MODIFIER LETTER CIRCUMFLEX ACCENT U+02C6 (letter ˆ)