2

I'm working on a macro to help me inserting figures into my document with configurable placement. I'm using the pgfkeys package for parsing key=value parameters passed to that macro, one of them being the placement parameter:

\documentclass{article}

\usepackage{float}
\usepackage[demo]{graphicx}
\usepackage{lipsum}
\usepackage{pgfkeys}

\pgfkeys{
    /fig/.is family, /fig,
    default/.style = {
        pos =,
    },
    pos/.estore in = \figPos,
}

\newcommand\mymacro[2][]{
    \pgfkeys{/fig, default, #1}
    {\em Placement parameter is: \figPos}
    \begin{figure}[\figPos]
        \includegraphics{#2}
    \end{figure}
}

\begin{document}

\section{Working as expected}

\lipsum[1]

\begin{figure}[t]
\includegraphics{image.jpg}
\end{figure}%

\lipsum[2]

\lipsum[3]

\section{Not working as expected}

\lipsum[1]

\mymacro[pos=t]{image.jpg}

\lipsum[2]

\lipsum[3]

\end{document}

When using the figure environment directly, the image gets positioned as expected, i.e. at the top of the page. However, when using the macro, the image gets included somewhere near the end of the document instead of close to its definition -- as though I had supplied an invalid placement parameter.

Any ideas and/or workarounds?

Werner
  • 603,163
  • figure doesn't expand the optional argument. Workarounds depend on what you want to do. – Ulrike Fischer May 04 '17 at 20:33
  • I think this has been covered before, the figure env does not expand the argument. But the default value is actually stored in an internal @ macro. If you are up for it, look in the memoir.cls source code for the string "tbp", you can set this macro and it will use that value if no []is given to the env. What memoir is doing is providing a user interface to set that default value. – daleif May 04 '17 at 20:33
  • In case you are trying to use your construct to define some default placement, you can instead use \def\fps@figure{t} – samcarter_is_at_topanswers.xyz May 04 '17 at 20:35
  • This is actually not about a default placement value; I'm working on a macro to help me inserting figures into my document with configurable placement. I'm using the pgfkeys package for parsing key=value parameters passed to that macro, one of them being the placement paramater: \mymacro[pos=t,foo=bar]{mumbo}{jumbo}. The \figPosmacro created by pgfkeys while parsing #1 gets ignored just as described above. – Torsten Crass May 04 '17 at 20:39
  • 1
    @TorstenCrass: Can you include the details you specify as part of a complete, minimal example that we can copy-and-paste-and-compile? It should replicate your issues. – Werner May 04 '17 at 20:44
  • @Werner, my initial posting was already an attempt to boil down my problem to its core. Anyway, I now edited my question to provide a complete, compilable example. – Torsten Crass May 04 '17 at 21:04

1 Answers1

2

Fundamentally, the optional argument for figure isn't fully expanded. You can expand it in the following way:

\newcommand\mymacro[2][]{%
  \pgfkeys{/fig, default, #1}%
  {\itshape Placement parameter is: \figPos}%
  \begingroup\edef\x{\endgroup\noexpand\begin{figure}[\figPos]}\x
    \includegraphics{#2}
  \end{figure}
}
Werner
  • 603,163
  • Yeah, the "figure environment doesn't expand its optional argument" was the clue for finding more information about this issue, like in (https://tex.stackexchange.com/questions/11336/macro-for-figure-position). Thanks anyway for providing a direct answer, which I will gratefully accept. – Torsten Crass May 04 '17 at 21:18