回避策としては、pdfcrop
(ターミナルで)個別に使用して、インポートする PDF ファイルをトリミングします。
しかし、ファイル内から PDF ファイルをインポートするときに白い余白を削除する方法はありますかtex
?
PDF ファイルをインポートするための一般的な 2 つのパッケージは、pdfpages
またはです。ファイル内でgraphicx
を使用して、それらのファイルを「前処理」できますか?pdfcrop
tex
答え1
\includegraphics
と同じように動作しますが、PDF イメージを切り抜く新しいコマンドです。
\newcommand{\includeCroppedPdf}[2][]{%
\immediate\write18{pdfcrop #2}%
\includegraphics[#1]{#2-crop}}
覚えて: \write18
有効にする必要があります。ほとんどの TeX ディストリビューションでは、/ etc を--shell-escape
実行するときにフラグを設定します。latex
pdflatex
例
\documentclass{article}
\usepackage{graphicx}
\newcommand{\includeCroppedPdf}[2][]{%
\immediate\write18{pdfcrop #2}%
\includegraphics[#1]{#2-crop}}
\begin{document}
\includeCroppedPdf[width=\textwidth]{test}
\end{document}
コンパイルごとに切り取るのを避ける
ドキュメントのコンパイルごとに切り取られるのを避けるには、切り取られたファイルがすでに存在するかどうかを確認します。(チェックサムがあるとさらに良いでしょう)
\documentclass{article}
\usepackage{graphicx}
\newcommand{\includeCroppedPdf}[2][]{%
\IfFileExists{./#2-crop.pdf}{}{%
\immediate\write18{pdfcrop #2 #2-crop.pdf}}%
\includegraphics[#1]{#2-crop.pdf}}
\begin{document}
\includeCroppedPdf[width=\textwidth]{test}
\end{document}
MD5 チェックサムの例
アイデアは、イメージの MD5 を保存し、次回の実行時に比較することです。これにはマクロが必要です(または\pdf@filemdfivesum
でのみ機能します)。 の場合は、ユーティリティを使用するか、ファイル diff を実行できます。PDFLaTeX
LuaLaTeX
XeLaTeX
\write18
md5sum
\documentclass{article}
\usepackage{graphicx}
\usepackage{etoolbox}
\makeatletter
\newcommand{\includeCroppedPdf}[2][]{\begingroup%
\edef\temp@mdfivesum{\pdf@filemdfivesum{#2.pdf}}%
\ifcsstrequal{#2mdfivesum}{temp@mdfivesum}{}{%
%file changed
\immediate\write18{pdfcrop #2 #2-crop.pdf}}%
\immediate\write\@auxout{\string\expandafter\string\gdef\string\csname\space #2mdfivesum\string\endcsname{\temp@mdfivesum}}%
\includegraphics[#1]{#2-crop.pdf}\endgroup}
\makeatother
\begin{document}
\includeCroppedPdf[width=\textwidth]{abc}
\end{document}
答え2
ファイル名パスにスペースがあるために問題が発生した場合、次の方法で問題を解決できます。
\documentclass{article}
\usepackage{graphicx}
\usepackage{etoolbox}
\makeatletter
\newcommand{\includeCroppedPdf}[2][]{\begingroup%
\edef\temp@mdfivesum{\pdf@filemdfivesum{"#2.pdf"}}%
\ifcsstrequal{#2mdfivesum}{temp@mdfivesum}{}{%
%file changed
\immediate\write18{pdfcrop "#2.pdf" "#2-crop.pdf"}}%
\immediate\write\@auxout{\string\expandafter\string\gdef\string\csname\space #2mdfivesum\string\endcsname{\temp@mdfivesum}}%
\includegraphics[#1]{"#2-crop"}\endgroup}
\makeatother
\begin{document}
\includeCroppedPdf[width=\textwidth]{./path to file with spaces/abc efg}
\end{document}
\graphicspath
注意: が変更された場合は機能しません