當圖像完全丟失時編譯但打開某些圖像?

當圖像完全丟失時編譯但打開某些圖像?

我正在嘗試編譯一個大型協作文檔,其中包括圖像中建模的輸出,這些圖像經常由其他協作者更新。我沒有得到這些圖像。我還是想把整個文件整理一下。

我一直在使用\usepackage[demo]{graphicx}這很好,因為即使圖像是文檔也會編譯完全地丟失的。這是如下建議的:當圖像遺失/不可用時編譯文件

不過,我確實想包含一些圖像(我有數據的圖像)。我想做一些事情,例如在使用時添加到\includegraphics[draft=false]{image}特定圖像\usepackage[draft]{graphicx}關閉並開啟圖中的影像。但是,此選項似乎不適用於\usepackage[demo]{graphicx}.

請注意,由於跨平台協作,團隊必須省略圖像擴展名,導致某些圖像帶有.PDF擴展名,而另一些則帶有.pdf.

有人知道這裡有好的解決方案嗎?

答案1

看來您預設提供圖片檔案副檔名。這很有幫助,因為graphics處理方式不同:如果您[不]提供映像副檔名且該檔案不存在,它只會建立一個警告[錯誤]。這在編譯方面有所不同,因為警告並不重要。

因此,我建議使用

\usepackage[draft]{graphicx}

這是一個最小的例子:

在此輸入影像描述

\documentclass{article}

\usepackage[draft]{graphicx}

\begin{document}

\includegraphics[width=100pt]{some-bizarre-image.png}% This does not exist

\includegraphics[width=150pt,draft=false]{example-image.png}% This exists (http://ctan.org/pkg/mwe)

\end{document}

如果您預設情況下不提供圖像檔案副檔名,則可能需要進行更新\includegraphics才能搜尋可能包含的檔案。假設您使用 pdfLaTeX 進行編譯,我們可以循環遍歷可能的映像檔副檔名:

在此輸入影像描述

\documentclass{article}

\usepackage[draft]{graphicx}
\usepackage{pgffor,letltxmacro}

% https://tex.stackexchange.com/q/72930/5764
% .png,.pdf,.jpg,.mps,.jpeg,.jbig2,.jb2,.PNG,.PDF,.JPG,.JPEG,.JBIG2,.JB2

\newif\iffilefound
\LetLtxMacro\oldincludegraphics\includegraphics
\renewcommand{\includegraphics}[2][]{%
  \global\filefoundfalse
  \foreach \fext in {,.png,.pdf,.jpg,.mps,.jpeg,.jbig2,.jb2,.PNG,.PDF,.JPG,.JPEG,.JBIG2,.JB2} {%
    \iffilefound\else\IfFileExists{#2\fext}{\global\filefoundtrue\xdef\imgfile{#2\fext}}{}\fi%
  }%
  \iffilefound
    \oldincludegraphics[#1]{\imgfile}%
  \else
    \oldincludegraphics[#1]{example-image}%
  \fi
}

\begin{document}

\includegraphics[width=100pt]{some-bizarre-image}% This does not exist

\includegraphics[width=150pt,draft=false]{example-image}% This exists (http://ctan.org/pkg/mwe)

\end{document}

我們循環遍歷所有可能的擴展(包括根本沒有擴展,如果您確實作為 的一部分手動提供了擴展\includegraphics),並將找到的第一個可用擴展識別為組合\imgfile.png如果圖像不存在,我們會插入一個虛構的(但現有的)檔案名稱的圖像。

相關內容