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.

I would like to create a few command that allows me to reference figures inside a table and add the reference to the table of contents.

\newcommand\fig[2]{
\addtocounter{figure}{1}
\includegraphics[width=3in]{#2}
\phantomsection
\addcontentsline{toc}{subsection}{Figure \ref{#1} #1}
\label{#1}
}

This is what I have tried, but the figure number does not seem to increment.

My goal is to generate something like in the screenshot below. Thanks for your help!!!

enter image description here

share|improve this question

2 Answers 2

If you use the capt-of package (just one line of code) then you don't need \refstepcounter and \addtocontents simply do

... &
\includegraphics{fig}
\captionof{figure}{the caption}\label{myfig}
& ...

and the figure will be captioned and added to the list of figures in the normal way.

share|improve this answer

The problem is how you're advancing the counter. You need to be using \refstepcounter to get the label to refer to the correct figure number.

Incidentally, you are allowing a lot of extra white space into your command. Try

\newcommand{\myfig}[2]
  {\refstepcounter{figure}%%'
   \includegraphics[width=2in]{#2}%%'
   \phantomsection
   \addcontentsline{toc}{subsection}{Figure \ref{#1} #1}%%'
   \label{#1}%%'
  }

To get figures to be numbered within sections as you image seems to suggest you need to add two lines to your preamble.

\usepackage{amsmath}
\numberwithin{figure}{section}
share|improve this answer
    
I think \csname phantomsection \endcsname is preferred. –  Jesse Aug 31 '13 at 4:10
    
@jesse I'm not sure I follow. \csname phantomsection \endcsname should do the same thing as \phantomsection; it's just a lot of typing. –  A.Ellett Aug 31 '13 at 21:17
    
indeed that is what I know of, but that produces an error while i follow your direction to try. Later on, i tried csname method and the error disappeared, thus the comment. –  Jesse Sep 1 '13 at 1:24
1  
@Jesse Now I understand your point. \csname ....\endcsname doesn't throw an error if the command name is not defined. The OP did not list all of the packages needed for his example. \phantomsection is a command defined in the hyperref package. If you load that package, then you should be able cut and paste my example without error. –  A.Ellett Sep 1 '13 at 1:29
    
It is clear now. Thanks for the remark above. –  Jesse Sep 1 '13 at 1:41

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.