1

I am struggling with moving the caption's location to the left. Is there a way to fix the alignment of the caption in the algorithm?

Below are some of the packages that I used and the code.

Thanks.

\usepackage[ruled, lined, linesnumbered, commentsnumbered, resetcount, longend]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{algorithm}
\caption{Offine Algorithm}\label{alg:offline}
\SetKwFunction{Send}{Send}
\SetKwFunction{Receive}{Receive}
\SetKwFunction{Verify}{Verify}
\SetKwFunction{Broadcast}{Broadcast}
\SetKwFunction{Store}{Store}
\SetKwInOut{KwIn}{Input}
\SetKwInOut{KwOut}{Output}

enter image description here

6
  • Welcome to TeX.SE...
    – MadyYuvi
    Commented May 31, 2022 at 3:54
  • Welcome to TeX! I added the line \SetAlCapHSkip{0ex} right before \caption, and it removed the space. Can you try this out? Commented May 31, 2022 at 3:54
  • Algorithm2e uses a custom caption. If you search here for "algorithm2e caption" you will find several questions, but I don't really understand the solutions. Commented May 31, 2022 at 3:55
  • Thank you all so much for the comments. I tried \SetAlCapHSkip{0ex} and it really helped!
    – june shin
    Commented May 31, 2022 at 3:57
  • @juneshin, did you try adding the line \SetAlCapHSkip{0ex} before \caption{Offine Algorithm}\label{alg:offline}? Did it solve the issue? Please let us know. Thank you! Commented May 31, 2022 at 4:01

1 Answer 1

2

A solution that worked

Add the code line:

\SetAlCapHSkip{0ex}

right before

\caption{Offine Algorithm}\label{alg:offline}

in your code to remove the horizontal space before the caption.

The full code example is:

\documentclass{article}
\usepackage[ruled, lined, linesnumbered, commentsnumbered, resetcount, longend]{algorithm2e}
\usepackage{algpseudocode}
\usepackage{caption}
\usepackage{subcaption}

\begin{document}

\begin{algorithm}
\SetAlCapHSkip{0ex} % <----------------- This is the line you should add.
\caption{Offine Algorithm}\label{alg:offline}
\SetKwFunction{Send}{Send}
\SetKwFunction{Receive}{Receive}
\SetKwFunction{Verify}{Verify}
\SetKwFunction{Broadcast}{Broadcast}
\SetKwFunction{Store}{Store}
\SetKwInOut{KwIn}{Input}
\SetKwInOut{KwOut}{Output}

\end{algorithm}

\end{document}

After you compile and run the code, it will look like this: enter image description here

Sources

This solution is taken from page 23 of the algorithm2e package documentation, which you can find at https://ctan.math.utah.edu/ctan/tex-archive/macros/latex/contrib/algorithm2e/doc/algorithm2e.pdf or access through https://ctan.org/pkg/algorithm2e?lang=en.

On this page, it says that

\SetAlCapHSkip{length} sets the horizontal skip before Algorithm: in caption when used in ruled algorithm.

You might find some additional useful macros there to boost your project.

Good luck!

2
  • 1
    This has nothing to do with the answer itself, save for saying that this is a very well written answer. Thanks.
    – Astrid
    Commented Sep 27, 2023 at 19:38
  • Thank you very much, @Astrid! Commented Sep 27, 2023 at 23:41

You must log in to answer this question.

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