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.
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)
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}
caption
package as well. A solution is provided by Adjusting the caption of analgorithm2e
code. The difference in "overfull\hbox
" measurement stem from the fact that you're using a12pt
default font, while the linked duplicate uses10pt
. The solution is to use\usepackage{etoolbox} \AtBeginEnvironment{algorithm}{\captionsetup{margin={-\algomargin,\algomargin}}}
. – Werner Sep 16 '14 at 23:22etoolbox
. 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:40algorithm2e
andcaption
. I think it may be something that should be fixed onalgorithm2e
's side. – Werner Sep 16 '14 at 23:46