First, I fully agree with Jesse Knight:
- Whoever made this requirement at your faculty is a sadist
But if you use LuaTeX you can destroy the glyph to unicode mapping in the PDF file by creating a special font feature:
% lualatex -shell-escape minted.tex
\begin{filecontents*}{test.rb}
# this should not be copyable
def hello
"world"
end
\end{filecontents*}
\documentclass[12pt,twoside,openright,a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage[dvipsnames]{xcolor}
\usepackage{minted}
\setmainfont{Times New Roman}
\usepackage{luacode}
\begin{luacode*}
local function nocopy(tfmdata)
for _, c in next, tfmdata.characters do
c.tounicode = nil
end
end
fonts.handlers.otf.features.register {
name = "nocopy",
description = "disallow copying for this font",
manipulators = {
base = nocopy,
node = nocopy,
plug = nocopy,
}
}
\end{luacode*}
\setmonofont[RawFeature=nocopy]{Latin Modern Mono}
\definecolor{bg}{rgb}{0.95,0.95,0.95}
\begin{document}
\chapter{Foo}
\section{Bar}
This should be copyable.
\inputminted[linenos, numbersep=5pt, tabsize=2, bgcolor=bg]{ruby}{test.rb}
And this again should be copyable.
\end{document}
With this approach you can not use the same font in the copyable and the not copyable part, so if you load any font once with nocopy
and once without, only the first setting will be respected.
If you do not want to affect "regular" \texttt text and verbatim material, you can select a special font for minted blocks:
% lualatex -shell-escape minted.tex
\begin{filecontents*}{test.rb}
# this should not be copyable
def hello
"world"
end
\end{filecontents*}
\documentclass[12pt,twoside,openright,a4paper]{report}
\usepackage[T1]{fontenc}
\usepackage{fontspec}
\usepackage[dvipsnames]{xcolor}
\usepackage{minted}
\setmainfont{Times New Roman}
\usepackage{luacode}
\begin{luacode*}
local function nocopy(tfmdata)
for _, c in next, tfmdata.characters do
c.tounicode = nil
end
end
fonts.handlers.otf.features.register {
name = "nocopy",
description = "disallow copying for this font",
manipulators = {
base = nocopy,
node = nocopy,
plug = nocopy,
}
}
\end{luacode*}
\newfontfamily\nocopyfont[RawFeature=nocopy]{Courier New}
\renewcommand\myFont{\nocopyfont}
\fvset{fontfamily=myFont}
\definecolor{bg}{rgb}{0.95,0.95,0.95}
\begin{document}
\chapter{Foo}
\section{Bar}
This should be copyable.
\inputminted[linenos, numbersep=5pt, tabsize=2, bgcolor=bg]{ruby}{test.rb}
And this again should be copyable (\texttt{This too}).
{
\tracingall\nocopyfont
}
\end{document}