LoginSignup
27
26

More than 5 years have passed since last update.

LaTeXから作るPDFファイルの数式部分からソースをコピーできるようにする

Last updated at Posted at 2018-05-12

LaTeXから作るPDFファイルの数式部分からソースをコピーできるようにする

やりたいこと:

  • LaTeXから作るPDFファイルの数式部分をなぞってコピー
  • テキストエディタにペーストしたら,数式部分のLaTeXソースが得られる ようにしたい.

必要なもの

  • LaTeX一式(今回私が試した環境はTeXLive2017同梱のlualatex)
  • PDFのビューワ(Adobe Acrobat Reader DC)
  • テキストエディタ(メモ帳,なんでもよい)

準備

ちょっとググると,TeX StackexchangeにMaking equations copyable in pdfという記事を見つけた.ほぼこれに従う.accsupp.styを使うが,lualatexと組み合わせて使うには,TeXLive2017に納められているものは古いようだ.CTANの accsupp – Better accessibility support for PDF files からversion 2018/01/30 v0.5のdtxファイルをダウンロードし,texでコンパイルして,styファイルを生成する.

先ほどの記事の解答の一つの2番目のマクロをいただいてくる.

結論

次のようなTeX文書で試してみる.lualatexで処理してPDFを生成する.

\documentclass[a4j,11pt]{ltjsarticle} %use lualatex.
\usepackage{luatexja}
\usepackage[yu-win10,no-math]{luatexja-preset}
\fontencoding{T1}
\usepackage[luatex,pdfencoding=auto]{hyperref}
\usepackage{accsupp}

% Taken from 
% https://tex.stackexchange.com/questions/119713/making-equations-copyable-in-pdf/119718#119718
\makeatletter
\newcommand*{\copyable}{%
  \begingroup
  \@sanitize
  \catcode`\%=14 % allow % as comment char, also needed for \%
  \@copyable
}
\newcommand*{\@copyable}[1]{%
  \endgroup
  \BeginAccSupp{%
    ActualText=\detokenize{#1},%
    method=pdfstringdef,
  }%
  \scantokens{#1}%
  \EndAccSupp{}%
}
\makeatother

\begin{document}
Copyable math formulas:
\[
  \copyable{\zeta(s) = \sum_{n=1}^\infty\frac{1}{n^s} = \prod_{p}\left(1-p^{-s}\right)^{-1},\quad \Re{s}>1.}
\]
\end{document}

結果は添付の図のようになる.スクリーンショット 2018-05-12 18.36.00.png

未解決な点

  • Adobe Acrobat Reader DCだと,マウスで選択する範囲が適切に表示されない(図の,薄青になっている箇所が,本来は数式部分全体になるべきである).
  • ペーストして得られるLaTeX数式の部分から '_' (下付を示すアンダースコア)が欠落している.

2番目の点は,'\detokenize' が返す文字コードが適切でないためだと思うが,解決できていない(そのためにLaTeXのソースでも'\fontenc{T1}'としているのだが).識者のご教示を待ちたい.

追記

repltext.sty でも同じことができる

CTANにあるrepltextを使っても同様のことができる.ただし,lualatexと一緒に使うには,pdftexのprimitiveでluatexにないものを補うpdftexcmdsも必要である.どちらもTeXLive2017には納められている.

\documentclass[a4j,11pt]{ltjsarticle} %use lualatex.
\usepackage{luatexja}
\usepackage{amsmath}
\usepackage[yu-win10,no-math]{luatexja-preset}
\fontencoding{T1}
\usepackage[luatex,pdfencoding=auto]{hyperref}
\usepackage{pdftexcmds}
\usepackage{repltext}
\makeatletter
\newcommand{\pdfescapestring}[1]{\pdf@escapestring{#1}}
\makeatother


\begin{document}
Copyable math formulas (using repltext.sty):
\begin{align*}
  \repltext{\zeta(s) &= \sum_{n=1}^\infty\frac{1}{n^s} = \prod_{p}\left(1-p^{-s}\right)^{-1},\;&\Re{s}>1,\\}{\prevrepl}
  \repltext{L(s, \chi) &= \sum_{n=1}^\infty\frac{\chi(n)}{n^s} = \prod_{p}\left(1-\chi(p)p^{-s}\right)^{-1},\; &\Re{s}>1.}{\prevrepl}  
\end{align*}

\end{document}

処理結果は下図のようになる.テキストエディタにペーストすると,やはり下付のunderscoreが反映されない他,余分な空白もできてしまうのが気になるところである.

スクリーンショット 2018-05-13 17.56.17.png

27
26
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
26