[Tex/LaTex] How to pass two parameters to a \newenvironment

floatsmacrosparameters

I have defined this new environment for handling my images in my document
I want to pass two parameters: #1 to refer to pathname to the imagefile #2 to refer to the string I want to pass to the label

 \newenvironment{insertmyimages}[2][,]%
 {
 \begin{figure}[!h]
 \centering
 \includegraphics[width=\textwidth]{#1}%
 \caption{Fig. #2}
 \end{figure}
 }

 \insertmyimages{./Image.gif, 17}

I would like the above to center the image and label this Fig. 17. How do I do this ?

Best Answer

This seems like a bit of a strange request- are you sure you don't want to have LaTeX generate the figure numbers for you?

If you really do want to do this, then here's one way- note that

  • I have changed \newenvironment to \newcommand
  • I have removed [,] which means something different from what you intended
  • I have used \setcounter to change the figure number to what you want

Here's a complete MWE

% arara: pdflatex
% !arara: indent: {overwrite: yes}
\documentclass{article}

\usepackage[demo]{graphicx}

\newcommand{\insertmyimages}[2]{%
    \setcounter{figure}{\numexpr#2-1\relax}
    \figure[!htb]
    \centering
    \includegraphics[width=\textwidth]{#1}%
    \caption{}
    \endfigure
}

\begin{document}
\insertmyimages{./Image.gif}{17}
\end{document}

Perhaps you might have wanted

\newcommand{\insertmyimages}[2]{%
    \figure[!htb]
    \centering
    \includegraphics[width=\textwidth]{#1}%
    \caption{}
     \label{#2}
    \endfigure
}

to be used as, for example

\insertmyimages{./Image.gif}{mylabel}
Related Question