我有一份文档,既可以打印出来,也可以在屏幕上显示。第一种情况下,图片应该是黑白的,另一种情况下,图片可以是彩色的。有了它,\newif
我可以轻松处理这个问题。
我在专用文件夹中存储了两个版本的图片,_c
彩色图片带有后缀。
我现在希望修补该\includegraphics[opt]{filename}
命令,以便
- 它首先检查
\filename@base _c\filename@ext
所提供的内容filename
(带或不带扩展名) - 如果文件不存在,则检查
\filename@base\filename@ext
- 包含相关图片,如果未找到文件则会引发错误。
我尝试\Ginclude@graphics
按照说明调整命令这里没有成功...为了编译下面的 MWE,只需在 cwd 中放置两张Image.…
图片。Image_c.…
% !TeX TS-encoding = utf8
% !TeX TS-spellcheck = fr_FR
% !BIB TS-program = biber
% !TeX TS-program = lualatex
\documentclass[12pt,a4paper]{memoir}
\usepackage{graphicx}
\makeatletter
\def\Ginclude@graphics#1{%
\begingroup
\let\input@path\Ginput@path
\filename@parse{#1}%
\ifx\filename@ext\relax
\@for\Gin@temp:=\Gin@extensions\do{%
\ifx\Gin@ext\relax
\Gin@getbase _c\Gin@temp
\fi
}%
\else
\Gin@getbase{\Gin@sepdefault\filename@ext}%
\ifx\Gin@ext\relax
\@warning{File `#1_c' not found}%
\def\Gin@base{\filename@area\filename@base _c}%
\edef\Gin@ext{\Gin@sepdefault\filename@ext}%
\fi
\fi
%%% add new test here?
\ifx\Gin@ext\relax
\filename@parse{#1}%
\ifx\filename@ext\relax
\@for\Gin@temp:=\Gin@extensions\do{%
\ifx\Gin@ext\relax
\Gin@getbase\Gin@temp
\fi
}%
\else
\Gin@getbase{\Gin@sepdefault\filename@ext}%
\ifx\Gin@ext\relax
\@warning{File `#1' not found}%
\def\Gin@base{\filename@area\filename@base}%
\edef\Gin@ext{\Gin@sepdefault\filename@ext}%
\fi
\fi
\fi
%%%
\ifx\Gin@ext\relax
\@latex@error{File `#1' not found}
{I could not locate the file with any of these extensions:^^J
\Gin@extensions^^J\@ehc}
\else
\@ifundefined{Gin@rule@\Gin@ext}%
{\ifx\Gin@rule@*\@undefined
\@latex@error{Unknown graphics extension: \Gin@ext}\@ehc
\else
\expandafter\Gin@setfile\Gin@rule@*{\Gin@base\Gin@ext}%
\fi}%
{\expandafter\expandafter\expandafter\Gin@setfile
\csname Gin@rule@\Gin@ext\endcsname{\Gin@base\Gin@ext}}%
\fi
\endgroup}
\makeatother
\begin{document}
\includegraphics[width=.7\linewidth]{Image}
\end{document}
答案1
这是一个解决方案。
\documentclass{article}
\usepackage{graphicx}
\newif\ifvcol
\vcoltrue
\let\WBCfile\relax
\makeatletter
\newcommand{\WBCincludegraphics}[2][]{%
\ifvcol
\@for\Gin@temp:=\Gin@extensions\do{%
\IfFileExists{#2_c\Gin@temp}{%
\def\WBCfile{#2_c\Gin@temp}}{}}%
\ifx\WBCfile\relax
\includegraphics[#1]{#2}%
\else
\includegraphics[#1]{#2_c}%
\fi
\else
\includegraphics[#1]{#2}%
\fi
\let\WBCfile\relax}
\makeatother
\begin{document}
\WBCincludegraphics[scale=.5]{example-image}
\end{document}
答案2
@touhami 解决了我的 OP。
但是,为了保留原始\includegraphics
命令,我将使用下面的代码片段(根据一些测试,它似乎有效)。因此我可以提供带或不带扩展名的文件名。
\documentclass[12pt,a4paper]{memoir}
\newif\ifvcol
\vcolfalse
\usepackage{graphicx}
\makeatletter
\def\Ginclude@graphics#1{%
\begingroup
\let\input@path\Ginput@path
\filename@parse{#1}%
\ifvcol
\filename@parse{\filename@area\filename@base _c}%
\@for\Gin@temp:=\Gin@extensions\do{%
\ifx\Gin@ext\relax
\Gin@getbase{\Gin@temp}
\fi}%
\fi
\ifx\Gin@ext\relax
\filename@parse{#1}%
\ifx\filename@ext\relax
\@for\Gin@temp:=\Gin@extensions\do{%
\ifx\Gin@ext\relax
\Gin@getbase{\Gin@temp}
\fi}%
\else
\Gin@getbase{\Gin@sepdefault\filename@ext}%
\ifx\Gin@ext\relax
\@warning{File `#1' not found}%
\def\Gin@base{\filename@area\filename@base}%
\edef\Gin@ext{\Gin@sepdefault\filename@ext}%
\fi
\fi
\fi
\ifx\Gin@ext\relax
\@latex@error{File `#1' not found}% <----------------------------- MODIFIED
{I could not locate the file with any of these extensions:^^J% <-- MODIFIED
\Gin@extensions^^J\@ehc}% <-------------------------------------- MODIFIED
\else
\@ifundefined{Gin@rule@\Gin@ext}%
{\ifx\Gin@rule@*\@undefined
\@latex@error{Unknown graphics extension: \Gin@ext}\@ehc
\else
\expandafter\Gin@setfile\Gin@rule@*{\Gin@base\Gin@ext}%
\fi}%
{\expandafter\expandafter\expandafter\Gin@setfile
\csname Gin@rule@\Gin@ext\endcsname{\Gin@base\Gin@ext}}%
\fi
\endgroup}
\makeatother
\newcommand{\fig}[2][.26]{{\par\centering\includegraphics[width=#1\linewidth]{#2}\par}}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\fig{Image}
\lipsum[2]
\includegraphics[height=2cm]{Image.png}
\end{document}