0

For easy and versatile integration of pictures and graphics in my documents I found a nice solution in this post 'newcommand for pictures'.

Due to unorthodox order of \begin{figure} and \end{figure} in the code I always have Texclipse (pdflatex, oxygen) showing errors at these lines (see code below}: \begin{figure} does not have matching end; at least one unbalanced begin-end. However, the code compiles with fine output.

How can I avoid getting these error messages with the functionality of the code pointed out in the post? If the code is fine, how can I suppress the errors messages in 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
    just put the code in mypic.sty and use \usepackage{mypic} in your docuemnt, then you can easily re-use in many documents and your editor will never see the code at all – David Carlisle Jul 6 '18 at 6:54
0

As David Carlisle pointed out, putting the code into a *.sty file does it. E.g. a working example is

\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}

with the sty file containing

\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

Here is some help for a good place of the sty file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.