예

해결 방법은 pdfcrop터미널에서 별도로 사용하여 가져오려는 PDF 파일을 자르는 것입니다.

그러나 파일 내에서 PDF 파일을 가져올 때 흰색 여백을 제거하는 방법이 있습니까 tex?

PDF 파일을 가져오는 두 가지 일반적인 패키지는 pdfpages또는 입니다 graphicx. 파일 pdfcrop내 에서 파일을 '전처리'할 수 있나요 tex?

답변1

\includegraphics와 유사하게 작동 하지만 PDF 이미지를 자르는 새로운 명령 :

\newcommand{\includeCroppedPdf}[2][]{%
    \immediate\write18{pdfcrop #2}%
    \includegraphics[#1]{#2-crop}}

기억하다: \write18활성화해야 합니다. 대부분의 TeX 배포판에서는 / 등을 --shell-escape실행할 때 플래그를 설정합니다.latexpdflatex

\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에서만 작동 ). 유틸리티 와 함께 ​​사용하거나 파일 비교를 수행할 수 있습니다 .PDFLaTeXLuaLaTeXXeLaTeX\write18md5sum

\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주의: 변경 되면 작동하지 않습니다.

관련 정보