如何製作一個能夠產生 PDF 的 LaTeX 文檔,使複製貼上能夠正確地處理空白行和前導空格縮排?

如何製作一個能夠產生 PDF 的 LaTeX 文檔,使複製貼上能夠正確地處理空白行和前導空格縮排?

我們正在用 LaTeX 編寫 PDF 文檔,其中包含各種安裝說明和其他技術文字。我們的想法是,使用者以 Serif 字體閱讀各種指令,然後將多行 Monotype 部分複製並貼上到控制台或編輯器中,即 Monotype 內聯是 shellscript、C 等。

最後我們比較喜歡那些 shell/C/等等。片段是未逃脫的(這就是 HEREDOC,<PRE>一種行為)。基於這個原因,我們嘗試了「listings」套件和\lstset+\begin{lstlisting}形式來實現文字內容的 HEREDOC 行為,即 Tex 檔案將包含文字而不使用任何轉義序列,然後我們嘗試\usepackage[space=true]{accsupp}將文字複製到 PDF按預期工作。

然而,嘗試這一點並沒有成功,我們不明白為什麼,具體來說,複製的文本往往會排除空白行和前導空格

最後詳細資訊:

  • 我們喜歡使用 IBM Plex Mono 作為科技字體。

  • 如果需要,我們可以容忍在每個空白行上添加一個空格。

  • 我們嘗試使用一些技巧使頁腳(以及側面的任何行號)不包含在複製貼上中,並且效果很好。

  • 如果複製貼上僅適用於某些特定的 PDF 檢視器,那也沒關係。如果是這樣,跨平台 PDF 檢視器之一會更受歡迎,它們是:Evince、Chrome、Firefox、MuPDF。 (如果還有更多想法,請分享。)有限平台的 PDF 檢視器是 Adob​​e Reader 和 PDF Exchange Viewer。

  • 如果Heredoc 風格在乳膠中是不可能的,那麼我們可以沒有它,例如每行一個 BeginAccsupp 或任何其他需要的措施。

您能分享一下您對此的想法和例子嗎?

如果這不能令人滿意地完成,您有何建議 - 使用其他輸出語言,例如由“htlatex”產生的 HTML?使用 PS 有什麼幫助,但如果有的話,有什麼查看軟體。

我們嘗試過的 Tex 程式碼

\LoadClass[a4paper,12pt,openright,oneside,chapter=TITLE,section=TITLE,hidelinks]
 {article}
\documentclass{article}
\RequirePackage[utf8]{inputenc}
\RequirePackage[a4paper,margin=2cm]{geometry}
\RequirePackage{plex-mono}
\usepackage[space=true]{accsupp}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{listings}
\RequirePackage{xcolor}
\lstset{ 
 % Configuration to make it unescaped and copy-paste right
 %upquote=true,
 %columns=fullflexible,
 %showstringspaces=true,
 %literate={\ }{{\copyablespace}}1,
 % For the looks - make it monotype etc
 %frame=tb,
 %aboveskip=3mm,
 %belowskip=3mm,
 %numbers=left,
 %numberstyle=\tiny\color{lightgray}\ttfamily, % \nonumbercopy,
 %keywordstyle=\color{green},
 %commentstyle=\color{blue},
 %stringstyle=\color{red},
 %breaklines=true,
 %breakatwhitespace=true,
 %tabsize=2,
 %numbersep=0.5em,
 %framerule=0pt,
 %xleftmargin=10pt,
 %xrightmargin=0pt,
 breaklines=true,
 backgroundcolor=\color{yellow},
 %keepspaces=true,
 basicstyle=\fontsize{9}{11}\ttfamily%,
 %columns=fixed,
 %fontadjust=true,
 %showlines=false,
 %showstringspaces=false
} 

%\newcommand{\copyablespace}{
%    \BeginAccSupp{method=hex,unicode,ActualText=0040}
%\ %
%    \EndAccSupp{}
%}

% Make the line numbers not appear in copy-paste
% \newcommand{\nonumbercopy}[1]{
%   \BeginAccSupp{method=hex,unicode,ActualText=00A0}#1\EndAccSupp{}
% }

% Footer
\renewcommand{\title}[1]{
 \pagestyle{fancy}               
 \lhead{}
 \rhead{}
 \cfoot{\squelch{\thepage}}
}


\begin{document}
    \noindent Beginning of document, serif outside unescaped part.
    \begin{lstlisting}
This is the uppermost-left unescaped monotyped text, it has no indentation.
  This is the second line, it has three letters of indentation
Here are some characters that prove that we can type without escaping: \{$
Now let's do an empty line:

The empty line worked fine. Copypaste this and see the copy is the same!
    \end{lstlisting}
    End of document, serif outside unescaped part.
\end{document}

參考

參考我們已經嘗試過但不起作用的方法:

有更多關於 AccSup 的討論:使表格內容可複製的技術

相關內容