1

I have a bunch of coordinates generated by a program and the text of the bodies of corresponding figures, e.g.:

\includegraphics{abc.png}
\caption{abc}
\label{fig:abc}

I was wondering if there is some way to place a figure at specified absolute coordinates (say, page 1, x = 245px, y=452px).

I have seen this question, but it doesn't work for figures (which don't go into boxes)

As for why I can't just use non-floats, I'm working with existing documents and don't want to go through everything and manually convert to something different. I was thinking maybe something like this would work:

\documentclass{article}
\usepackage{capt-of}

\newcommand{\fakefigure}[1]{
    \renewcommand{\label}[1]{}
    \renewcommand{\caption}[1]{\captionof{figure}{#1}}
    #1
}

\begin{document}

\fakefigure{
    \includegraphics{abc.png}
    \caption{abc}
    \label{fig:abc}
}
\end{document}

And then I could put that in the box from here. But I'm not sure if there are some other commands I need to trap to make that work.

  • There is but you must not put it in a figure environment; which flots by definition. For the caption, you can use \captionof{figure}{caption text}. – Bernard May 20 '16 at 21:50
  • @Bernard I am doing this programmatically so trying to strip out everything seems difficult. If I were to say \def\caption{\captionof{fig:abc} and \renewcommand\label[1]{} would it work? – k_g May 20 '16 at 21:53
  • 2
    Please provide a complete minimal example which we can copy-paste-compile to help you. Certainly what you just suggested won't work. For one thing, you're missing a curly bracket. More generally, what would that be meant to do? Note that the primary problem here is the use of the figure environment. You need not to use it. You don't even have to have a caption if you don't want. If you do want, that's a secondary issue. The main point is: if you don't want something to float, don't make it a float. A figure is a float. Floats, by design, float. That's kind of their raison d'être. – cfr May 20 '16 at 22:49
  • @cfr I can't just not use floats because I'm trying to modify existing documents and want to make as few changes as possible. – k_g May 20 '16 at 23:34
  • @k_g - Sorry to have to say it, but the answer is "your objective cannot be achieved with figure environments". No way, no how. Time to start looking into using the \captionof macro. – Mico May 20 '16 at 23:37
2

Here's a way of making the \fakefigure macro suggested in the question work. Well, at least it survives minimal testing in combination with Werner's answer on absolute placement.

The proposed redefinition of \caption won't work because capt-of defines \captionof in terms of \caption. However, it is possible to first save the definition of \caption and then use the saved definition when redefining \caption.

\global\let\oldcaption\caption
\renewcommand\caption{\def\@captype{figure}\oldcaption}% modified from capt-of

I've only allowed for figure type elements here. If you need other kinds of floats to be converted to non-floats, you'd want to reinstitute the optional argument provided by capt-of.

We can then combine this with Mico's suggestion to create the \fakefigure{} macro:

\makeatletter
\global\let\oldcaption\caption
\newcommand{\fakefigure}[1]{%
  \begin{minipage}{\textwidth}
    \renewcommand\caption{\def\@captype{figure}\oldcaption}% modified from capt-of
    #1
  \end{minipage}%
}
\makeatother

This then allows us to write something like

\placetextbox{.35}{,3}{%
  \fakefigure{%
    \includegraphics[height=.2\textheight]{tiger}%
    \caption{abc}%
    \label{fig:abc}%
  }%
}

to place TeX's standard tiger at an arbitrary position on the page.

tiger at arbitrary position

\documentclass{article}
\usepackage{graphicx,kantlipsum,eso-pic}
\makeatletter
\global\let\oldcaption\caption
\newcommand{\fakefigure}[1]{%
  \begin{minipage}{\textwidth}
    \renewcommand\caption{\def\@captype{figure}\oldcaption}% modified from capt-of
    #1
  \end{minipage}%
}
\makeatother
% from Werner's answer at https://tex.stackexchange.com/a/24675/
\newcommand{\placetextbox}[3]{% \placetextbox{<horizontal pos>}{<vertical pos>}{<stuff>}
  \setbox0=\hbox{#3}% box
  \AddToShipoutPictureFG*{% Add <stuff> to current page foreground
    \put(\LenToUnit{#1\paperwidth},\LenToUnit{#2\paperheight}){\vtop{{\null}\makebox[0pt][c]{#3}}}%
  }%
}
\begin{document}
\placetextbox{0.5}{0.5}{\fbox{\Huge\textsf{This is my text.}}}
\placetextbox{0.5}{1}{\Huge\texttt{Here is another piece of text.}}
\placetextbox{0.1}{0.1}{\Large $\mathcal{A}_1$}
\placetextbox{.35}{,3}{%
  \fakefigure{%
    \includegraphics[height=.2\textheight]{tiger}%
    \caption{abc}%
    \label{fig:abc}%
  }%
}
\kant[1-3]
\end{document}
2

If I understand what you're looking for, you may want to pursue the following approach: Using the facilities of your text editor,

  • globally replace all instances of \begin{figure} with

    \par\noindent\begin{minipage}{\textwidth}%
    
  • globally replace all instances of \end{figure} with

    \end{minipage}
    

    and

  • globally replace all instances of figure-related \caption statements with

    \captionof{figure}
    

    If your document has any table environments, don't modify the table-related \caption statements.

What this will still be lacking, relative to a (now no-longer floating) figure environment, is the vertical spacing LaTeX would insert above and below the environment, as needed. As you seem to want to place these environments in arbitrary places on a page, I trust this won't be a shortcoming.

  • Thanks, I'll try that out and hopefully it works. (Actually, I'm writing a program to do this, but hopefully the process is the same) – k_g May 21 '16 at 0:05
  • @k_g - If you can use LuaLaTeX and are familiar with the basics of Lua programming, it's straightforward to program up a Lua function that acts as a preprocessor and does these replacements before LaTeX starts its normal processing. – Mico May 21 '16 at 0:07
  • Yeah, I'm actually using python for the rest of it, and this doesn't seem all that complicated. But can I set the exact position of a minipage? – k_g May 21 '16 at 0:11
  • 1
    @k_g - There are quite a few postings on this site about to go about achieving exact positioning of various objects on a page. – Mico May 21 '16 at 0:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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