![使用 if-else 宏生成 \includegraphics 的路径](https://linux22.com/image/376869/%E4%BD%BF%E7%94%A8%20if-else%20%E5%AE%8F%E7%94%9F%E6%88%90%20%5Cincludegraphics%20%E7%9A%84%E8%B7%AF%E5%BE%84.png)
我希望有一个选项/宏(真/假或类似)来确定从哪个文件夹中选择图。每个图只有 2 个文件夹。主要需要它在低分辨率/高分辨率图之间切换,以加快写作时查看/编译文档的速度。使用低分辨率图查看速度更快(我使用相当旧的笔记本)。我想出了一些代码,但它给出了这些错误:1) File ended while scanning use of
__xparse_grab_G_trailing:w。2) ==>Fatal error occurred, no output PDF file produced!
如果我使用\includegraphics
带有硬编码路径的注释行,它当然可以起作用。
该testPlot.png
文件位于plots/smallPlots
文件夹下。我通常也使用\addpic
宏(它生成\includegraphics
代码)代替\includegraphics
,但即使是这个简化版本也不起作用。
谢谢你!
示例代码:
\documentclass{article}
\usepackage{xparse}
\usepackage{graphicx}
\usepackage{etoolbox}
\newcommand{\IsUseFullSizePlots}{yes} % Path switching parameter. Macro either defined or commented
\NewDocumentCommand{\Path}{g}{%
plots/\ifdef{\IsUseFullSizePlots}{originalFullSizePlots}{smallPlots} % from etoolbox package
}
\begin{document}
Path = \Path % checking
\includegraphics[width=.3\textwidth, clip]{\Path/testPlot}
% \includegraphics[width=.3\textwidth, clip]{plots/smallPlots/testPlot}
\end{document}
宏的代码\addpic
(不太明白它是如何工作的,但可以工作:):
\ExplSyntaxOn
% the user level command
\NewDocumentCommand{\addpic}{m}
{
\group_begin: % localize the changes to the variables
\simonson_pic:n { #1 }
\group_end:
}
% the key-value interface
\keys_define:nn { simonson/pic }
{
placement .tl_set:N = \l_simonson_pic_placement_tl,
placement .initial:n = htp,
width .tl_set:N = \l_simonson_pic_width_tl,
width .initial:n = 1,
options .tl_set:N = \l_simonson_pic_options_tl,
image .tl_set:N = \l_simonson_pic_image_tl,
caption .tl_set:N = \l_simonson_pic_caption_tl,
shortcaption .tl_set:N = \l_simonson_pic_shortcaption_tl,
label .tl_set:N = \l_simonson_pic_label_tl,
}
% the main command
\cs_new_protected:Nn \simonson_pic:n
{
% set the keys from the argument
\keys_set:nn { simonson/pic } { #1 }
% start the figure environment
\__simonson_start_figure:V \l_simonson_pic_placement_tl
\centering
% include the image
\__simonson_pic_image:VVV
\l_simonson_pic_width_tl % the text width fraction
\l_simonson_pic_options_tl % other options
\l_simonson_pic_image_tl % the image name
% the caption
\tl_if_empty:NTF \l_simonson_pic_shortcaption_tl
{
\caption{\l_simonson_pic_caption_tl}
}
{
\caption[\l_simonson_pic_shortcaption_tl]{\l_simonson_pic_caption_tl}
}
% the label
% In \ addpics \ macro:\ label=\l_simonson_pic_label_tl %for test(my code)
\tl_if_empty:NF \l_simonson_pic_label_tl
{
\label{\l_simonson_pic_label_tl}
}
% end the figure environment
%### %for parser to ignore
\end{figure}
%###
}
% syntactic sugar: we want some token lists to be expanded before usage
\cs_new_protected:Nn \__simonson_start_figure:n
{
\begin{figure}[#1]
}
\cs_generate_variant:Nn \__simonson_start_figure:n { V }
\cs_new_protected:Nn \__simonson_pic_image:nnn
{
\includegraphics[width=#1\textwidth,#2]{#3}
}
\cs_generate_variant:Nn \__simonson_pic_image:nnn { VVV }
\ExplSyntaxOff
================================================================ 使用示例
\addpic{
width=0.3,
image=example-image,
caption={This is an example image, and a comma in the caption},
label=one,
}
答案1
一种选择是使用 if/then 逻辑来设置\GraphicsPath
,然后\includegraphics
不需要更改。(只要不同文件夹中的文件名相同)。
答案2
问题在于您对 的定义\Path
,带有一个(未使用的)可选参数。
做就是了
%\newcommand{\Path}{plots/originalFullSizePlots} % for final version
\newcommand{\Path}{plots/smallPlots} % for draft
并在您想要制作最终版本时切换评论。
调用\addpic
应该是这样的
\addpic{
width=0.3,
image=\Path/testPlot,
caption={This is an example image, and a comma in the caption},
label=one,
}