3

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 ?

4
  • Please add a minimal working example (MWE) Jul 16, 2013 at 9:47
  • \centering won't do anything if you use width=\textwidth; the image has nowhere to go. Jul 16, 2013 at 9:56
  • looks to me as if you need a command. what you've written could be more concisely expressed as a command, though i don't quite understand it (it seems to make the file name optional, with default ","). also, your caption will come out as "Figure <number>: Fig. <second arg>" which is surely not right. Jul 16, 2013 at 9:58
  • I want the caption to be Figure <number> or Fig <number>. Either one would work. Your observation is correct. I was under the impression that "," is how you separate the #1 and #2, when you use the macro within your document. That was a mistake as well.
    – user33731
    Jul 16, 2013 at 13:20

1 Answer 1

4

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}
3
  • Many thanks for your valuable input and suggestion. You mentioned that LaTeX can create the figure number automatically. Can you LMK how this can be accomplished ? Many thanks for your prompt response. I think I can make progress in the meantime.
    – user33731
    Jul 16, 2013 at 12:57
  • Ok. I re-used the code. Looks like I am almost there. I seem to get "Figure 1:" under the first figure and so on. Is there a way to get rid of the trailing ":" ? If I got "Figure 1", i think I would be done. Also, I have used \usepackage[pdftex]{graphicx}; using \usepackage[demo]{graphicx} produces a black rectangle of the size of the image. My document class is memoir.
    – user33731
    Jul 16, 2013 at 13:17
  • @user33731 Add \captiondelim{} to the preamble (if you want to suppress the colon and space from the labels for all floats) or to the definition of the new command inside figure and before \caption (for just figures). Jul 16, 2013 at 13:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .