6

This is a follow up to here.

I am using this in my preamble:

\makeatletter   % inserts a "\centering" to every floatbox.
\g@addto@macro\@floatboxreset\centering
\makeatother

I am having two question for the use of this:

  • How can I implement the same thing for subfigures, too? It works fine for all floats, but not for minipages or subfigures (from subcaption, which are minipages as well).
  • How can I switch of this behavior locally?

Here is some code for playing around:

% arara: pdflatex
% arara: pdflatex

\documentclass{article}
\usepackage[demo]{graphicx}

\makeatletter
\g@addto@macro\@floatboxreset\centering
\makeatother

\begin{document}
\begin{figure}%
    \begin{minipage}[b]{.48\linewidth}
        \centering
        \includegraphics[width=.5\linewidth]{qwertz}
    \end{minipage}
    \hfill
    \begin{minipage}[b]{.48\linewidth}
        %\centering
        \includegraphics[width=.5\linewidth]{qwerty}
    \end{minipage}
    \caption{Caption}%
    \label{fig:label} 
\end{figure}%
\end{document}
7

If you're willing to use subcaption, then

\g@addto@macro\subcaption@minipage\centering

should work for subfigure and subtable environments.


Added November 13, 2013

A revision of subcaption has made \subcaption@minipage a macro with arguments, so the above patch doesn't work any more and

\usepackage{etoolbox}

\makeatletter
\apptocmd\subcaption@minipage{\centering}{}{}
\makeatother

becomes necessary.


I wouldn't add \centering to the code for general minipage environments, even locally in a float, because they might pop up in unexpected places when using macros that exploit minipage in their code.

You could use a “local” version of \g@addto@macro for adding \centering to all minipage environments in a float; \appto of etoolbox is good for this:

\usepackage{etoolbox}
\makeatletter
\gappto\@floatboxreset{\centering\appto\@minipagerestore{\centering}}
\makeatother

but I wouldn't do it, as this would mean that all minipages in a float would get \centering; there are macros that internally use minipage that might be used in a float: also those would receive \centering, which could so end up in unwanted places.

  • Yes, I am willing. And always do. So thank you very much for solution and explanation. The point "even locally in a float" is not clear to me. Shouldn't I use the syntax provided in my OP? Do you have a solution for my second question? I will happily mark this as an answer then. – LaRiFaRi Sep 24 '13 at 15:15
  • @LaRiFaRi I've added how you could do it; but, please, don't, for the reasons I explain. – egreg Sep 24 '13 at 15:37
  • I'm sorry, I wasn't aware that someone is using the internal macro \subcaption@minipage otherwise I would have tried to avoid that change. However, I think introducing something like \@subfloatboxreset which could be patched more easily would be a good idea!? – Axel Sommerfeldt Nov 14 '13 at 8:18
  • @AxelSommerfeldt That's really a good idea. Maybe also an interface for getting automatic centering? It seems wanted by many people. – egreg Nov 14 '13 at 9:47
6

You could insert the following instructions in the preamble of your document to center-set the contents of table, figure, subtable, and subfigure environments. To affect the positioning of the latter two environments, it is assumed that the subcaption package is loaded.

\usepackage{etoolbox}
\makeatletter
\patchcmd{\@xfloat}%
  {\@floatboxreset}%
  {\@floatboxreset\centering}{}{}
\patchcmd{\subcaption@minipage}%
  {\setcaptionsubtype}%
  {\centering\setcaptionsubtype}{}{}
\makeatother

The first macro that's patched, \@xfloat, is defined in the LaTeX kernel; the second patched macro, \subcaption@minipage, is provided by the subcaption package.

  • @krnk - The diagnostic remark "didn't work" isn't very specific. Which document class do you use? Do you load a package, such as subcaption, that provides environments named subtable and subfigure? – Mico Jan 19 '15 at 9:34
  • @krnk - Thanks for pointing out that the code was no longer working. (I think the subcaption package was revised substantially in late 2013, but I hadn't taken note of the consequences for my answer until just now.) I've rewritten the patching commands; please let me know if things are OK now. – Mico Jan 19 '15 at 16:10

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.