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.

Consider a float of image with

\begin{figure}[h]
    \includegraphics[width=1.0\textwidth]{ebooks.jpg}
    \captionof{figure}{This is a test}
    \label{fig: 1}
\end{figure}

Is it possible to define the figure at the beginning of the document, and automatically displaying the figure after first description of the figure (when term Figure X appears on the text for the first time)?

In fact, it should look for the first occurrence of the term Figure X to place the figure close to that.

In general, I want to know how to search for a term in the text and display a figure close to that.

share|improve this question
3  
Not in any of the standard packages that I know of. flafter package (in the base latex distribution) stops floats floating forward so if you put the figure by its first reference it will never go before that (ie won't float to the top of that page). Also of course never use [h] as that just means "take float to end of document". –  David Carlisle Feb 28 '12 at 2:11
    
@DavidCarlisle: could you explain the part about [h]? –  mbork Mar 2 '12 at 13:52
3  
@mbork floats normally default to [tbp] that is they are allowed at the top or bottom of pages, and if too many floats build up latex can make a float page pand put them there. If you put [h] then the figure might fit at that place but usually it won't as it is too far down the page for example. but [h] means not t not b or not p. especially disallowing p floats gives LaTeX no chance to position the float at all so it is held to the end when it just dumps all the floats on the final pages as it can't do anything else. –  David Carlisle Mar 2 '12 at 14:15
add comment

1 Answer

up vote 3 down vote accepted

One possible way to get this to work is to keep each figure in a separate file name identical to the label in a fig/ directory. Then modify the \ref macro to include the figure at that point if it has not already been included.

To allow for multiple references to the same figure, we need to only include the image for the first time it is referenced and not for any subsequent references.

To keep things simple I have defined a new macro below \figref to do this and the figure appears on Page 3, and does not appear again upon subsequent references:

enter image description here

Notes:

  • To run the test case below as is, the directory fig/ must already exist in the directory where you are running this from
  • I used the filecontents environment to package this test case into one file, and am not suggesting to use this to create the individual files for the figures, although you certainly could do that.
  • Although I defined a new macro, it certainly should be possible to just redefine the existing \ref macro to extract out the part before and after the colon, and act appropriately for the case where the part before the colon is fig.

Further Enhancements:

  • Below I defined a command name every time a figure is inserted to suppress duplicate images. Since the label process already makes a check for multiply-defined labels, this code could be enhanced by reusing the same method.

Code:

\documentclass{article}
\usepackage{graphicx}% Remove [demo] for real usage
\usepackage{lipsum}% For dummy text
\usepackage{xcolor}
\usepackage{caption}
\usepackage{filecontents}

\begin{filecontents*}{fig/1.tex}
    \begin{figure}[htbp]
        \centering
        \includegraphics[width=0.5\textwidth]{images/EiffelWide.jpg}
        \captionof{figure}{This is a test}
        \label{fig: 1}
    \end{figure}
\end{filecontents*}

\makeatletter%
\newcommand{\figref}[1]{%
    \ifcsname @LoadedFile#1\endcsname%
    \else%
        \input{fig/#1}%
        \global\expandafter\def\csname @LoadedFile#1\endcsname{}%
    \fi%
    Figure~\ref{fig: #1}%
}%
\makeatother%

\begin{document}
\lipsum[1-11]

\textcolor{red}{Now as we can see from \figref{1} we can conclude.}

\textcolor{red}{But what if again refer to \figref{1}, the figure should not be included again!!}
\end{document}
share|improve this answer
    
very useful and handy trick; though I am not sure if it is the best way to do so. However, it solved my problem perfectly. Thanks! –  All Mar 12 '12 at 22:42
1  
@Ali: Updated solution so that figure only appears once. Previous version would include the figure each time it was referenced, which is probably not desired. –  Peter Grill Mar 13 '12 at 2:14
    
very important important point that I was not noticed. Anyway, I like your idea of filecontents, it can be used for insertion of quotation boxes! –  All Mar 13 '12 at 7:27
add comment

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.