# Macro for image including

I was wondering if there is a work around, or better way, to include images. I am using a simple macro that helps me:

My main.tex

\documentclass{article}
\usepackage{graphicx}
\input{images.tex}
\begin{document}

Hello! Look an awesome image Fig.\ref{fig:AwesomeImage_1_22.png}.
\fooFig{AwesomeImage_1_22.png}

\end{document}


My images.tex contains:

\makeatletter
\newcommand{\DefinefooFig}[2]{%
\expandafter\newcommand\csname foofig#1\endcsname{#2}%
}
\newcommand{\fooFig}[1]{\csname foofig#1\endcsname}
\makeatother

\DefinefooFig{AwesomeImage_1_22.png}{
\begin{figure}[p]
\centering
\includegraphics[width=0.8\textwidth]{image.png}
\caption{Awesome Image}
\label{fig:AwesomeImage_1_22.png}
\end{figure}
}


In my main content file I can include \fooFig{AwesomeImage_1_22.png} which helps me to keep my chapters clean and easy readable and small.

However, i have stumbled upon many forum posts that discourage the use of \expandafter! So i am asking if there is a better way to do this!

Best -z

• What should the output of \DefinefooFig{<image>}{<text>} be? – Werner Nov 30 '14 at 18:20
• Welcome to TeX.SX! You should give some more information on the purposes of your command and provide a \documentclass{}...\begin{document}...\end{document} frame to work with – user31729 Nov 30 '14 at 18:31
• @Werner: Two reviews??? – user31729 Nov 30 '14 at 18:36
• @ChristianHupfer: That's weird... I posted the comment and forgot to submit the review... Then you must have completed your review before I did that. – Werner Nov 30 '14 at 19:19
• @zwck: So the output of \DefinefooFig is nothing by the creation of another macro that you can use somewhere else? That doesn't make sense. – Werner Nov 30 '14 at 19:20

I don't think you gain too much with this approach, but you're the best judge about it. I find that having the caption in an external file makes it difficult to modify it.

There is nothing bad in the definition you have, apart too dramatic indentation:

\newcommand{\DefinefooFig}[2]{%
% define a new command to contain code for a figure
\expandafter\newcommand\csname foofig#1\endcsname{#2}%
}
% define a command for inserting a figure
\newcommand{\fooFig}[1]{\csname foofig#1\endcsname}

\DefinefooFig{AwesomeImage_1_22.png}{%
\begin{figure}[p]
\centering
\includegraphics[width=0.8\textwidth]{image.png}
\caption{Awesome Image}
\label{fig:AwesomeImage_1_22.png}
\end{figure}%
}


Just add the % in the places I did.


You could use



and



but it's just syntactic sugar.

A slightly different approach that lets you specify the positional argument at run time, rather than in the external file:

\newcommand{\DefinefooFig}[2]{%
% define a new command to contain code for a figure
\expandafter\newcommand\csname foofig#1\endcsname[1]{%
\begin{figure}[##1]
#2
\end{figure}
}%
}
% define a command for inserting a figure
\newcommand{\fooFig}[2][htp]{\csname foofig#2\endcsname{#1}}

\DefinefooFig{AwesomeImage_1_22.png}{%
\centering
\includegraphics[width=0.8\textwidth]{image.png}
\caption{Awesome Image}
\label{fig:AwesomeImage_1_22.png}
}


Note that the figure environment is not any more in the code of a \DefinefooFig. This allows you to have

\fooFig{AwesomeImage_1_22.png}


for the default placement, or

\fooFig[!htbp]{AwesomeImage_1_22.png}


(any other positional argument will do, of course) if you want to override the htp default.

• Thats a great addition. Is there no way to achieve the same macro without the \expandafter ? – zwck Nov 30 '14 at 20:42
• @zwck No: you need to form the token before \def (or other similar command) sees it. – egreg Nov 30 '14 at 20:44