5

I've a question about the alignment of the caption in an algorithm-environment. For instance, the following code-example

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\begin{document}
\begin{algorithm}[t]
\caption{Text Text\newline Text}
\begin{algorithmic}[1]
\State bla
\end{algorithmic}
\end{algorithm}
\end{document}

produces enter image description here

However, the third "Text" should be directly under the first "Text". How can I accomplish this?

  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – karlkoeller Mar 13 '15 at 10:54
4

You can use the caption package with a hanging indent style as follows:

Sample output

Longer sample

\documentclass{article}

\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{caption}

\captionsetup[algorithm]{format=hang,singlelinecheck=false}

\begin{document}

\begin{algorithm}[t]
\caption{Text Text\newline Text}  
\begin{algorithmic}[1]
\State bla
\end{algorithmic}
\end{algorithm}

\begin{algorithm}[t]
\caption{Text text text text text text text text text text text text
text text text text text text text text text text text text\newline
More text}
\begin{algorithmic}[1]
\State bla
\end{algorithmic}
\end{algorithm}

\end{document}
|improve this answer|||||
  • Thank you both for your answers. Why do you use singlelinecheck=false? This doesn't change anything in my captions. – Texmex Mar 13 '15 at 11:23
  • 1
    Without singlelinecheck=false the first example with a short caption doesn't get a hanging indent after the \newline. – Andrew Swann Mar 13 '15 at 11:26
  • I, too, have tried with format=hang but I forgot about singlelinecheck=false :( +1. BTW: hangindent=0pt is superflous. :-) – karlkoeller Mar 13 '15 at 11:38
  • 2
    @karlkoeller You are right about the hangindent=0pt; the internals of the caption set up styles do indeed set this to 0pt, so it is unnecessary and I have now removed it from the code. It would be nice if the documentation told us about the default value. – Andrew Swann Mar 13 '15 at 11:44
3

Load the caption package and declare:

\DeclareCaptionFormat{custom}{%
\begin{minipage}[t]{0.2\linewidth}\vspace*{0pt}#1#2\end{minipage}%
\begin{minipage}[t]{0.8\linewidth}\vspace*{0pt}#3\end{minipage}%
}

After that,

\captionsetup[algorithm]{format=custom}

MWE:

\documentclass{article}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}

\usepackage{caption}
\DeclareCaptionFormat{custom}{%
\begin{minipage}[t]{0.2\linewidth}\vspace*{0pt}#1#2\end{minipage}%
\begin{minipage}[t]{0.8\linewidth}\vspace*{0pt}#3\end{minipage}%
}
\captionsetup[algorithm]{format=custom}

\begin{document}
\begin{algorithm}[t]
\caption{Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text Text\newline Text on a new line}
\begin{algorithmic}[1]
\State bla
\end{algorithmic}
\end{algorithm}

\end{document} 

enter image description here

|improve this answer|||||

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.