为了轻松、灵活地将图片和图形集成到我的文档中,我找到了一个很好的解决方案这篇文章“图片的新命令”。
由于代码中的\begin{figure}
和的顺序非常不规则,我总是让 Texclipse(pdflatex、oxygen)在这些行显示错误(参见下面的代码):。但是,代码可以编译并输出良好的输出。\end{figure}
\begin{figure} does not have matching end; at least one unbalanced begin-end
我怎样才能避免使用代码中指出的功能收到这些错误消息邮政? 如果代码没有问题,我该如何抑制 Texclipse 中的错误消息?
\documentclass{article}
\usepackage{graphicx}
\usepackage{xparse}
% Between the \ExplSyntaxOn the code is copied from
% https://tex.stackexchange.com/questions/242271/newcommand-for-pictures
\ExplSyntaxOn
% the user level command
\NewDocumentCommand{\addpic}{m}
{
\group_begin: % localize the changes to the variables
\forthisdoc_pic:n { #1 }
\group_end:
}
% the key-value interface
\keys_define:nn { forthisdoc/pic }
{
placement .tl_set:N = \l_forthisdoc_pic_placement_tl,
placement .initial:n = htp,
width .tl_set:N = \l_forthisdoc_pic_width_tl,
width .initial:n = 1,
options .tl_set:N = \l_forthisdoc_pic_options_tl,
image .tl_set:N = \l_forthisdoc_pic_image_tl,
caption .tl_set:N = \l_forthisdoc_pic_caption_tl,
shortcaption .tl_set:N = \l_forthisdoc_pic_shortcaption_tl,
label .tl_set:N = \l_forthisdoc_pic_label_tl,
}
% the main command
\cs_new_protected:Nn \forthisdoc_pic:n
{
% set the keys from the argument
\keys_set:nn { forthisdoc/pic } { #1 }
% start the figure environment
\__forthisdoc_start_figure:V \l_forthisdoc_pic_placement_tl
\centering
% include the image
\__forthisdoc_pic_image:VVV
\l_forthisdoc_pic_width_tl % the text width fraction
\l_forthisdoc_pic_options_tl % other options
\l_forthisdoc_pic_image_tl % the image name
% the caption
\tl_if_empty:NTF \l_forthisdoc_pic_shortcaption_tl
{
\caption{\l_forthisdoc_pic_caption_tl}
}
{
\caption[\l_forthisdoc_pic_shortcaption_tl]{\l_forthisdoc_pic_caption_tl}
}
% the label
\tl_if_empty:NF \l_forthisdoc_pic_label_tl
{
\label{\l_forthisdoc_pic_label_tl}
}
% end the figure environment
\end{figure}
}
% syntactic sugar: we want some token lists to be expanded before usage
\cs_new_protected:Nn \__forthisdoc_start_figure:n
{
\begin{figure}[#1]
}
\cs_generate_variant:Nn \__forthisdoc_start_figure:n { V }
\cs_new_protected:Nn \__forthisdoc_pic_image:nnn
{
\includegraphics[width=#1\textwidth,#2]{#3}
}
\cs_generate_variant:Nn \__forthisdoc_pic_image:nnn { VVV }
\ExplSyntaxOff
\begin{document}
An example picture is shown in \ref{label}.
\addpic{
placement=bp,
width=0.2,
options={angle=90},
image=example-image-a,
caption=Rotated image,
label=label,
shortcaption=In the text the image is rotated!,
}
\end{document}
答案1
作为大卫·卡莱尔指出,将代码放入 *.sty 文件即可。例如,一个工作示例是
\documentclass[]{article}
\usepackage{addpic}
\begin{document}
\addpic{
width=0.3,
image=example-image,
caption={This is an example image, and a comma in the caption},
label=one,
}
\end{document}
包含 sty 文件
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{addpic}[2018/12/03 addpic by tex.stackexchange example]
\RequirePackage{graphicx}
\RequirePackage{xparse}
% Between the \ExplSyntaxOn the code is copied from
% https://tex.stackexchange.com/questions/242271/newcommand-for-pictures
\ExplSyntaxOn
% the user level command
\NewDocumentCommand{\addpic}{m}
{
\group_begin: % localize the changes to the variables
\forthisdoc_pic:n { #1 }
\group_end:
}
% the key-value interface
\keys_define:nn { forthisdoc/pic }
{
placement .tl_set:N = \l_forthisdoc_pic_placement_tl,
placement .initial:n = htp,
width .tl_set:N = \l_forthisdoc_pic_width_tl,
width .initial:n = 1,
options .tl_set:N = \l_forthisdoc_pic_options_tl,
image .tl_set:N = \l_forthisdoc_pic_image_tl,
caption .tl_set:N = \l_forthisdoc_pic_caption_tl,
shortcaption .tl_set:N = \l_forthisdoc_pic_shortcaption_tl,
label .tl_set:N = \l_forthisdoc_pic_label_tl,
}
% the main command
\cs_new_protected:Nn \forthisdoc_pic:n
{
% set the keys from the argument
\keys_set:nn { forthisdoc/pic } { #1 }
% start the figure environment
\__forthisdoc_start_figure:V \l_forthisdoc_pic_placement_tl
\centering
% include the image
\__forthisdoc_pic_image:VVV
\l_forthisdoc_pic_width_tl % the text width fraction
\l_forthisdoc_pic_options_tl % other options
\l_forthisdoc_pic_image_tl % the image name
% the caption
\tl_if_empty:NTF \l_forthisdoc_pic_shortcaption_tl
{
\caption{\l_forthisdoc_pic_caption_tl}
}
{
\caption[\l_forthisdoc_pic_shortcaption_tl]{\l_forthisdoc_pic_caption_tl}
}
% the label
\tl_if_empty:NF \l_forthisdoc_pic_label_tl
{
\label{\l_forthisdoc_pic_label_tl}
}
% end the figure environment
\end{figure}
}
% syntactic sugar: we want some token lists to be expanded before usage
\cs_new_protected:Nn \__forthisdoc_start_figure:n
{
\begin{figure}[#1]
}
\cs_generate_variant:Nn \__forthisdoc_start_figure:n { V }
\cs_new_protected:Nn \__forthisdoc_pic_image:nnn
{
\includegraphics[width=#1\textwidth,#2]{#3}
}
\cs_generate_variant:Nn \__forthisdoc_pic_image:nnn { VVV }
\ExplSyntaxOff