3

I noticed that the height of the caption of the algorithm environment does not solely depend on the font size, but on the actual caption text as well:

\documentclass{scrartcl}

\usepackage[ngerman]{babel}
\usepackage[no-math]{fontspec}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage{microtype}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}
  \caption{Euclid's algorithm (Ü)}
  \label{euclid}
  \begin{algorithmic}[1]
    \Procedure{Euclid}{$a,b$}
    \State $r\gets a\bmod b$
    \While{$r\not=0$}
      \State $a\gets b$
      \State $b\gets r$
      \State $r\gets a\bmod b$
    \EndWhile
    \State \textbf{return} $b$
    \EndProcedure
  \end{algorithmic} 
\end{algorithm}

\end{document}

When you remove the (Ü) from the caption, the height/alignment changes:

caption alignment

How can I achieve a consistent height and alignment that does not depend on the actual glyphs? Something like \protect\vphantom{Ü} seems to fix it in this case, but surely there is some more elegant and generic approach.

2
  • Is this really an issue? Unless you're using beamer overlays where the jumping would be annoying I doubt somebody will use a ruler and measure the distance of those two caption rules
    – user31729
    Jan 3, 2016 at 0:02
  • @ChristianHupfer Fair enough, but even if you ignore the (Ü) in my animated image for a second, I think the overall spacing looks far more balanced in this "extended" version. I think that's because the mean line is centered between the two caption rules.
    – ph4nt0m
    Jan 3, 2016 at 9:06

1 Answer 1

1

algorithms.sty loads float.sty to define the algorithm environment. caption.sty supports customizing of captions defined with float.sty. You can use the strut key provided by caption.sty for a consistent height within captions. Code below is without algorithms.sty to show what happens behind the scenes:

\documentclass{article}

\usepackage{float}
\floatstyle{ruled}
\floatname{algorithm}{Algorithm}
\newfloat{algorithm}{htbp}{loa}

\usepackage{caption}
\captionsetup[ruled]{strut=on}

\begin{document}

\begin{algorithm}
\captionsetup{strut=off}
\caption{Euclid's algorithm}
\begin{verbatim}
\Procedure{Euclid}{$a,b$}
\State $r\gets a\bmod b$
\While{$r\not=0$}
  \State $a\gets b$
  \State $b\gets r$
  \State $r\gets a\bmod b$
\EndWhile
\State \textbf{return} $b$
\EndProcedure
\end{verbatim}
\end{algorithm}

\begin{algorithm}
\caption{Euclid's algorithm}
\begin{verbatim}
\Procedure{Euclid}{$a,b$}
\State $r\gets a\bmod b$
\While{$r\not=0$}
  \State $a\gets b$
  \State $b\gets r$
  \State $r\gets a\bmod b$
\EndWhile
\State \textbf{return} $b$
\EndProcedure
\end{verbatim}
\end{algorithm}

\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .