3

I have a project which makes use of the subcaption and algorithm2e packages. For whatever reason, the subcaption package is causing the captions in the algorithms to be misplaced.

Here we have a MWE of the issue. Note that the algorithm2e package is defined with the [figure] option.

\documentclass[12pt]{book}
\usepackage{subcaption}
\usepackage[figure,tworuled]{algorithm2e}
\begin{document}
\begin{algorithm}
\DontPrintSemicolon
$f_1$ = Fase a calcular\;
\tcp{Início e fim do período considerado}
$t_1$ = Idade química do concreto na fase anterior à $f_1$\;
$t_2$ = Idade química do concreto na fase $f_1$\;
\caption{Cálculo das perdas por fluência do concreto para uma fase}
\label{fig:AlgFluencia}
\end{algorithm}
\end{document}

Which results in the image below. Note that the caption is clearly not centered, with the text almost entirely flushed to the right.

enter image description here

If I then comment out the \usepackage{subcaption} line, I get the result below. The caption is nice and centered, no problems. (though there does seem to still be more space to the left than the right, though that might just be me. Regardless, its clearly better)

enter image description here

If the [figure] option in algorithm2e is removed, then there is no conflict between the packages, but I'd much rather keep it. The documentation on algorithm2e doesn't point to anything in regards to this (that I saw)... any suggestions?

EDIT 1: After seeing Werner's link to a solution, I thought all was solved, but thought too soon. Unfortunately, unlike the MWE shown, my project must in fact adopt an university-defined class file which makes use of atbeginend. With this package the same error occurs and is not corrected by the solution liked to be Werner. A more precise MWE (including Werner's solution) follows, along with the resulting image. It still makes use of the book class just to allow compilation by others, but the addition of atbeginend is sufficient to give the error.

\documentclass[a4paper,12pt]{book}
\usepackage{atbeginend}
\usepackage[figure,tworuled,linesnumbered]{algorithm2e}
\usepackage{subcaption}
\usepackage{etoolbox}
\AtBeginEnvironment{algorithm}{%etoolbox
  \captionsetup{margin={-\algomargin,\algomargin}}%
}
\begin{document}
\begin{algorithm}
\DontPrintSemicolon
$f_1$ = Fase a calcular\;
\tcp{Início e fim do período considerado}
$t_1$ = Idade química do concreto na fase anterior à $f_1$\;
$t_2$ = Idade química do concreto na fase $f_1$\;
\caption{Cálculo das perdas por fluência do concreto para uma fase}
\end{algorithm}
\end{document}

enter image description here

  • This can be replicated with only the caption package as well. A solution is provided by Adjusting the caption of an algorithm2e code. The difference in "overfull \hbox" measurement stem from the fact that you're using a 12pt default font, while the linked duplicate uses 10pt. The solution is to use \usepackage{etoolbox} \AtBeginEnvironment{algorithm}{\captionsetup{margin={-\algomargin,\algomargin}}}. – Werner Sep 16 '14 at 23:22
  • Thanks, that solves it. In my actual project I actually already had etoolbox. Would this be considered a bug in the interaction of the packages or can it somehow be considered a "feature?" Also, while I understand its because the answer is the same as the one in the link, I gotta say... I hate that duplicate stamp. – Wasabi Sep 16 '14 at 23:40
  • 1
    There would definitely be a debate around whether a duplicate is considered a duplicate because of the question or the answer. It's probably a bad interaction between algorithm2e and caption. I think it may be something that should be fixed on algorithm2e's side. – Werner Sep 16 '14 at 23:46
  • 1
    We'd like to keep answers separate from questions, so you should write a separate answer instead of editing your answer into the question. Self-answers are perfectly admissible, and a well-written answer may earn you additional reputation. – Paul Gessler Mar 29 '15 at 22:22
  • 5
    I’m voting to close this question because it was solved in the comments and the OP documented the answer by editing his question instead of self-answering. – BambOo Jun 1 at 22:11
1

The solution ended up being to face the music and alter the university's class file, replacing atbeginend with etoolbox, which contains similar commands.

  • \AfterBegin{document}{-code-} became \AfterEndPreamble{-code-}
  • Any other \AfterBegin{-env-}{-code-} became \AtBeginEnvironment{-env-}{-code-}
  • \BeforeBegin{-env-}{-code-} became \BeforeBeginEnvironment{-env-}{-code-}
  • \BeforeEnd{-env-}{-code-} became \AtEndEnvironment{-env-}{-code-}

This along with @Werner's solution in the comments solved the issue.


Thank you to @BambOo for reminding me to write this as an answer, this question was from my starting days on SE.

| improve this answer | |

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