2

Is it somehow possible to have different \captionsetup depending on the floating position a table ends up with?

In the following MWE I'd like to have \captionsetup{belowskip=0pt} automatically for all tables which end up to be placed at the top of a page in order to remove the space marked by the red arrow:

enter image description here

\documentclass{book}

\usepackage{graphicx}
\usepackage{lipsum}

\usepackage{caption}
\captionsetup*[table]{position=above, belowskip=\baselineskip}

\usepackage[showframe]{geometry}

\begin{document}

\lipsum[2]
\begin{table}[htbp]
\caption{test}
\end{table}
\lipsum[2-6]
\begin{table}[htbp]
%\captionsetup{belowskip=0pt}
\caption{test}
\end{table}
\lipsum[2]

\end{document}
  • 1
    the entire table is set before it is positioned so in general it's a bit tricky but you can do a multi-pass thing which records the typeset position on the previous run, or I have an answer somewhere that typesets all the versions and picks one to use as the float is positioned, I'll see if I can find it... – David Carlisle Mar 08 '18 at 17:28
  • https://tex.stackexchange.com/questions/56017/formatting-floats-differently-based-on-placement/87861#87861 – David Carlisle Mar 08 '18 at 17:31
  • @DavidCarlisle That looks very promising! Thanks for the link. I will test and report back (might take a bit, a lecture is about to start ...) – samcarter_is_at_topanswers.xyz Mar 08 '18 at 17:32
  • If you can this space you also change the size of the table so the placement could perhaps change. Wouldn't it make more sense to set the value to 0 and to change \textfloatsep etc to change the spacing around the floats? – Ulrike Fischer Mar 08 '18 at 17:32
  • @UlrikeFischer Already thought about this. I'm willing to compensate for this space somewhere else in the table, maybe at the bottom. I will also try your suggestion with \textfloatsep. – samcarter_is_at_topanswers.xyz Mar 08 '18 at 17:35
  • @UlrikeFischer Your suggestion about the \textfloatsep went exactly in right direction. Modifying \intextsep gave me the desired output. Would you like to add an answer? – samcarter_is_at_topanswers.xyz Mar 08 '18 at 18:42
  • @DavidCarlisle Your \floatswitch works perfectly! ... until one loads the caption package. Then it reports true for bottom and top for each float. Nevertheless thank you very much for finding your previous answer! – samcarter_is_at_topanswers.xyz Mar 08 '18 at 18:43
  • @samcarter yes well it's not that surprising it doesn't work out of the box with caption or float packages, but probably fixable if required, not sure I have time at present but someone might take that hint and try something... – David Carlisle Mar 08 '18 at 18:57
  • @DavidCarlisle Don't worry, for now my problem is solved with @ UlrikeFischers comment. – samcarter_is_at_topanswers.xyz Mar 08 '18 at 19:00

1 Answers1

1

The comment of @Ulrike Fischers comment put me on track to find a workaround. The basic idea is to modify \intextsep instead of trying to do this via the caption package.

A nice overview of the spaces around floats can be found at Remove space after figure and before text

\documentclass{book}

\usepackage{graphicx}
\usepackage{lipsum}

\usepackage{caption}
\captionsetup*[table]{position=above, 
belowskip=0pt,
}
\setlength{\intextsep}{\baselineskip}

\usepackage[showframe]{geometry}

\begin{document}

\lipsum[2]
\begin{table}[htbp]
\caption{test}
\centering
\includegraphics[width=1cm]{example-image}
\end{table}
\lipsum[2-6]
\begin{table}[t]
\caption{test}
\centering
\includegraphics[width=1cm]{example-image}
\end{table}
\lipsum[2]

\clearpage
\lipsum[2]
\begin{figure}[htbp]
\centering
\includegraphics[width=1cm]{example-image}
\caption{test}
\end{figure}
\lipsum[2-6]
\begin{figure}[t]
\centering
\includegraphics[width=1cm]{example-image}
\caption{test}
\end{figure}
\lipsum[2]

\end{document}