Algorithm caption left alignment

algorithm2ealgorithmsalignmentcaptions

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

Best Answer

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!