Graphics, Figures & TablesFloat placement ignored when passed via macro

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
woj-k
Posts: 15
Joined: Wed Jul 28, 2010 7:25 pm

Float placement ignored when passed via macro

Post by woj-k »

Hello everyone,

I have come across an interesting problem with floats. As I was writing some wrapper environments, I noticed that the optional position argument cannot be processed correctly, if it is first stored and then retrieved from a temporary `storage' macro. In th MWE below, myfloatone ignores the position, and myfloattwo is fine with it. The only difference is how the optional argument is passed to the figure environment.

Code: Select all

\documentclass{article}

\newenvironment{myfloatone}[3][tbp]{%
\def\postemp{#1}\def\captemp{#2}\def\labtemp{#3}%
\begin{figure}[\postemp]}%          <- Difference here
{\caption{\captemp}\label{\labtemp}\end{figure}}

\newenvironment{myfloattwo}[3][tbp]{%
\def\postemp{#1}\def\captemp{#2}\def\labtemp{#3}%
\begin{figure}[#1]}%                <- Difference here
{\caption{\captemp}\label{\labtemp}\end{figure}}

\begin{document}
One

Two

\begin{myfloatone}[b]{caption one}{labelone}
\rule{1em}{1em}
\end{myfloatone}

Three

\clearpage
One

Two

\begin{myfloattwo}[b]{caption two}{labeltwo}
\rule{1em}{1em}\ \rule{1em}{1em}
\end{myfloattwo}

Three
\end{document}
Changing \def to \edef does nothing. The problem exists both when using latex and pdflatex.

Can anyone offer an explanation? And, more importantly, suggest how I could pass the position argument via another macro so that the float takes it in? Thank you in advance for any advice.
Last edited by woj-k on Wed Jul 17, 2013 1:30 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Float placement ignored when passed via macro

Post by cgnieder »

The macro in the option needs to be expanded before the table reads it. Maybe something like this can help:

Code: Select all

\documentclass{article}

\makeatletter
\def\ExpEnvOption#1#2[#3]{%
  \expandafter\@expenvopt\expandafter{#3}{#1}{#2}}
\def\@expenvopt#1#2#3{#2{#3}[#1]}
\makeatother

\def\options{h}

\begin{document}
text

\ExpEnvOption
\begin{table}[\options]
  table
\end{table}

text
\end{document}
Regards
site moderator & package author
woj-k
Posts: 15
Joined: Wed Jul 28, 2010 7:25 pm

Re: Float placement ignored when passed via macro

Post by woj-k »

It worked! Your solution is trivially integrated into the MWE and solves the problem.

I would probably spend a good few more weeks fooling around with \expandafter's; switching arguments around and reading+inserting the square brackets in a separate step is a new trick to me. I will think on how to use it to my advantage in my existing code.

Thank you for your help!
Post Reply