Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

What i'm trying to do is to set up a quick insert of a pdf at a default size with the new command \zfh. The problem lies with the label and how it's listed in my label list database.

example: \zhf{FileLocation}{xpicture} my label list has "fig_ #3" instead of listing "fig_ xpicture"

I'm new to latex, any help would be appreciated and if there's a better method to confronting this problem, i'd be glad to hear it.

\documentclass[10pt,a4paper]{article}

\usepackage{graphicx}

\begin{document}

\newcommand{\zfh}[3][2in]{
    \begin{figure}[H]
        \begin{center}
           \includegraphics[height= #1]{./FT/#2}
        \end{center}
        \caption{\protect\detokenize{#3}}
        \label{fig_  #3}
    \end{figure}
    }

    \zfh{zzz.pdf}{XCaption}

    \ref{fig_  #3}  % fig_ #3 is listed instead of the actual text inserted as the #3 arg
\end{document}
share|improve this question
1  
@PeterGrill has answered the question but it's best not to use a space in the ref argument so I would use fig_#3 and ref{fig_XCaption}without a space in both cases. Also you should use \centering rather than \begin{center} in this context to avoid extra vertical space, and finally it is best not to use [H] which is only really as a last-resort final edit for difficult figures, it should never be used as here hidden in another markup for a range of figures. –  David Carlisle Jul 10 '12 at 21:32
1  
Also the \protect\detokenize is nothing I would recommend. –  egreg Jul 10 '12 at 21:46
    
what you had in mind with \ref{fig_ #3} is not possible. #3 is not known outside of \zfh –  Herbert Jul 11 '12 at 6:24
    
@Herbert: thanks, I thought so, but i thought i'd ask before I gave up. –  Zlaid Jul 11 '12 at 14:15

2 Answers 2

Probably you are using some kind of intelligent editor that tries to find \label commands in the TeX source and offer a list of label names if you are typing \ref. The problem is that the \label occurs inside a definition and your editor only sees \label{fig_#3}, but ignores the calls of \zfh. Your editor is not TeX, thus it does not know that \newcommands starts a definition and that it must replace #3 by the third argument. I do not know, which editor you are using, but I have some doubts that it can be taught easily, how to deal with \zfh. But maybe you can trick your editor by changing the way, \zfh is called and also the \label inside the definition can perhaps be hidden.

\documentclass[10pt,a4paper]{article}

\usepackage{graphicx}
\usepackage{float}

\newcommand*{\RemoveFigPrefix}{}%
\def\RemoveFigPrefix#1_{}

\newcommand*{\zfh}[2][2in]{%
  \zfhLabel{#1}{#2}%
}
\newcommand*{\zfhLabel}{}
\edef\zfhLabel{%
  \def\noexpand\zfhLabel##1##2%
  \expandafter\noexpand\csname label\endcsname
}\zfhLabel#3{%
  \begin{figure}[H]% see comments
    \centering % see comments
    \includegraphics[height=#1]{./FT/#2}
    \caption{\RemoveFigPrefix#3}
    \csname label\endcsname{#3}%
  \end{figure}%
}

\begin{document}

  \listoffigures

  \section*{Section with figures}

  \zfh[1in]{zzz.pdf}\label{fig_XCaption}

  \zfh[1in]{foo.pdf}\label{fig_YPic}

  Two figures \ref{fig_XCaption} and \ref{fig_YPic}.

\end{document}

Some explanations:

  • Some macro definitions are done using TeX's \def, because it allows more powerful parameter texts. But it does not check, whether the command is already defined, it just overwrites a previous meaning of the macro. Therefore the \newcommand lines are added that give LaTeX a chance to issue an error message, if the macro is already defined.

  • The \label inside the definition is hidden using \csname and \endcsname. These commands construct command tokens:

    \csname label\endcsname => \label 
    
  • If the editor reads \zfh{...}{...}\label{...} then it will understand \label{...}. But we want to reuse the argument of \label for the caption. Therefore the \label{...} is a required part of the syntax of \zfh and gets consumed during the execution of \zfh. Nevertheless \zfh will eventually call \label{...}.

  • To make the definition of \zfh easier, the definition is split up in two macros. The first scans for the optional argument and the first mandatory argument; the second macro \zfhLabel takes care of the \label{...} part. In the definition of \zfhLabel there is another \label that might confuse the editor (Or not, because the curly braces are missing, but who knows for sure?):

    \def\zfhLabel#1#2\label#3{%
    

    This is obfuscated by

    \edef\zfhLabel{%
      \def\noexpand\zfhLabel##1##2%
      \expandafter\noexpand\csname label\endcsname
    }\zfhLabel#3{%
    

    There is no need to understand this for a beginner. The more experienced will see that the first definition of \zfhLabel is just a temporary definition that constructs the part of the final definition of \zfhLabel.

Let's us hope that your editor now finds all correct \label commands with the changes in the TeX source code above, ignores the \label inside definitions and presents you a complete label list, if you want to reference a figure.

share|improve this answer

To fix you main problem you need to use \ref{fig_ XCaption} to refer to the figure. The second optional parameter to \zfh is what is used for the label, so that is what needs to be used in the \ref{}, not #3.

In the MWE below, I have incorporated the suggestions of David Carlisle and egreg:

Code:

\documentclass[10pt,a4paper]{article}

\usepackage[demo]{graphicx}

\newcommand{\zfh}[3][2in]{
    \begin{figure}
        \centering
        \includegraphics[height= #1]{./FT/#2}
        \caption{#3}
        \label{fig_#3}
    \end{figure}
    }

\begin{document}
    \zfh{zzz.pdf}{XCaption}

    See Figure~\ref{fig_XCaption}  % Figure 1 is inserted as desired
\end{document}
share|improve this answer
    
Although the tips are thoughtful, it's not the main problem. The code, with it's lack of efficiency, worked. The problem is the fact that my label database/collection has "fig_#3" instead of "fig_XCaption" listed. I can very well type \ref{fig_XCaption}, but that would suck out most of the handyness of latex documents... –  Zlaid Jul 11 '12 at 1:36
    
Not sure what you mean. #3 refers to the third parameter of a macro and does not exist outside of the macro. If you move the \ref{fig_#3} to be in the \zfh then you can use #3, but that isn't of much use. Perhaps if you explained exactly what you are trying to do, then someone here might be able to provide a suggestion for improvement. –  Peter Grill Jul 11 '12 at 4:11
    
@Zlaid: Basically, the question is to know how TeX should choose which figure to refer to: what happens if you have more than one instance of \zfh? –  Bruno Le Floch Jul 11 '12 at 11:58
    
Even after invoking \zfh twice, my label database only references fig_ #3. for say: \zfh{xfile.pdf}{Xpic}\zfh{yfile.pdf}{Ypic} my database/list of labels list only fig_ #3. And the nice popup menu that shows up when i usually \ref something, doesnt include fig_Xpic nor fig_Ypic, only fig_ #3. –  Zlaid Jul 11 '12 at 14:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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