발행된 목록에서 텍스트 복사 비활성화

발행된 목록에서 텍스트 복사 비활성화

내 작성 목록에 복사 가능한 텍스트가 없도록 하고 싶습니다. 나는 이것을 발견했습니다 :

복사할 수 없는 텍스트가 포함된 PDF를 생성할 수 있습니까?

하지만 몇 시간 동안 시험해 본 후에도 내 문서와 통합할 수 없습니다. 저는 간단한 LaTeX report문서를 사용하고 있으며 XeTeX 또는 LuaLaTeX를 사용하여 빌드할 수 있습니다.

예시 문서:

% 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}
\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}

문서

복사한 텍스트를 다음과 같이 표시하고 싶습니다.

Chapter 1 Foo
1.1 Bar
This should be copyable.

And this again should be copyable.

답변1

첫째, 나는 Jesse Knight의 말에 전적으로 동의합니다.

  • 너희 교수진에서 이런 요구사항을 만든 사람은 새디스트야

그러나 LuaTeX를 사용하는 경우 특수 글꼴 기능을 생성하여 PDF 파일에서 글리프-유니코드 매핑을 파괴할 수 있습니다.

% 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}

이 접근 방식을 사용하면 복사 가능한 부분과 복사할 수 없는 부분에 동일한 글꼴을 사용할 수 없으므로 글꼴이 있거나 nocopy없는 글꼴을 한 번 로드하면 첫 번째 설정만 존중됩니다.

"일반" \texttt 텍스트 및 축어적 자료에 영향을 주지 않으려면 생성된 블록에 대한 특수 글꼴을 선택할 수 있습니다.

% 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}

답변2

해결책

아마도 나의 가장 큰 해킹일 것입니다 ...

main.tex: --shell-escape허용하도록 컴파일\write18기리다

\documentclass[12pt,twoside,openright,a4paper]{report}
\usepackage{graphicx}
\newcommand{\nocopycode}[2]{%
  \immediate\write18{./code.sh #1 #2}
  \\[\textfloatsep]
  \includegraphics[width=\linewidth]{{#2}.png}%
  \\[\textfloatsep]
}
\usepackage{listings}
\begin{document}
\chapter{Foo}
\section{Bar}
This should be copyable.
\nocopycode{ruby}{hello.rb}
And this again should be copyable.
\end{document}

code.shchmod +x code.sh: 스크립트가 실행되도록 허용 해야 할 수도 있습니다.

echo \\def\\xstyle{$1} \\def\\xfile{$2} > code-config.tex
pdflatex -interaction=nonstopmode code.tex
convert -trim -density 300 code.pdf $2.png

code.tex: convert어디에이미지매직

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{
  basicstyle=\ttfamily,
  backgroundcolor=\color{lightgray},
}
\input{code-config.tex}
\begin{document}
  \pagenumbering{gobble}
  \lstinputlisting[language={\xstyle}]{\xfile}
\end{document}

... 생산 main.pdf:

산출

노트:

  • 마음껏 가지고 놀면서 code.tex원하는 스타일을 얻으세요.
  • 코드 이미지에 앨리어싱 아티팩트가 있을 수 있지만 우리가 할 수 있는 일은 많지 않습니다.
  • 코드가 많으면 문서 크기가 커집니다.
  • Windows에서 이 작업을 수행하도록 요청하지 마세요.
  • 너희 교수진에서 이런 요구사항을 만든 사람은 새디스트야

답변3

제가 귀하의 상황에 처해 있다면 다음 3단계를 따르겠습니다.

  1. 조판된 발행 목록을 PDF로 내보내기(동일한 페이지에 맞는 경우 독립 실행형, 목록이 여러 페이지에 걸쳐 있는 경우 다른 문서 클래스 사용),
  2. 고스트스크립트를 실행하여 텍스트를 벡터 경로로 변환합니다.
  3. 생성된 환경의 위치에 ghostscript로 생성된 벡터 경로 pdf를 가져옵니다.

다음은 예입니다(독립 실행형 포함).

  1. 1단계: 작성된 모든 코드가 독립형 파일에 있다고 가정해 보겠습니다. 인라인 코드를 별도의 PDF로 내보내는 다른 방법이 있을 수 있지만 방법은 모르겠습니다.
% minted.tex
\documentclass{standalone}
\usepackage{minted}
\begin{document}
\begin{minipage}[t]{0.5\textwidth}
\begin{minted}{bash}
echo "Hello, world!"
\end{minted}
\end{minipage}
\end{document}

>>lualatex --shell-escape minted.tex좋아하는 라텍스 엔진을 실행하세요 . 저는 . 다음과 같은 minted.pdf가 생성됩니다. 복사 가능한 텍스트

  1. 다음 명령을 사용하여 다음 스크립트를 실행합니다.>>./fonttopath.sh minted.pdf mintedpath.pdf
% fonttopath.sh
#!/bin/sh
if [ "x$1" = "x" -o "x$2" = "x" ]; then
        echo Usage: `basename "$0"` "<input.pdf>" "<output.pdf>" >&2
        exit 1
fi
gs -o "$2" -sDEVICE=pdfwrite -dNOCACHE -dNoOutputFonts -dNOSUBSTDEVICECOLORS -c 60000000 setvmthreshold -f "$1"
  1. 생성된 코드를 만들려는 위치에 mintedpath.pdf를 가져옵니다.*

이 솔루션의 장점: 1. 아무것도 래스터화되지 않습니다. 2. JPEG 등과 같은 이미지 형식을 내보내고 가져오는 것에 비해 파일 크기가 더 작습니다. 3. 인쇄 품질이 저하되지 않습니다.4. 귀하의 의견에서 요청한 대로 이는 여러 페이지에 걸쳐 있는 긴 코드 블록에도 적용됩니다. 즉, 독립 실행형 대신 기사와 같은 문서 클래스를 사용하고 보고서의 기하학적 구조와 일치하도록 페이지의 기하학적 구조를 설정하는 경우입니다. Ghostscript는 PDF 스패닝 페이지를 여러 페이지에 걸쳐 있는 동등한 벡터 경로 PDF로 변환한 다음 라텍스 코드에 다른 PDF 문서를 추가하는 것처럼 가져올 수 있습니다.

  • 참고: PDF 뷰어에 따라 이러한 벡터 경로를 선택할 수 있지만 워드 프로세서나 편집기에 붙여넣어 보면 텍스트가 복사되지 않는다는 것을 알 수 있습니다(이 시점에서는 텍스트가 남아 있지 않기 때문입니다).

관련 정보