[Tex/LaTex] Horizontal spacing in caption in algorithm2e

algorithm2emarginsspacing

In algorithm2e, how can I shift the caption to the left? There is a small space that I need to remove.

Here is my code:

\documentclass[letterpaper]{article}
\usepackage[linesnumbered,ruled]{algorithm2e}

\begin{document}

\begin{algorithm}[t]
Initialize 
\\ 
Do stuff ...
\caption{Example sdfkjdshf ksdjhf dskjfhssdkjfhds kfsjh 1kjh kjh kjh d}
\label{alg:example}
\end{algorithm}

\end{document}

UPDATE
Even if there is space for some characters, the caption goes on a new line. Why? How can I fix that?

Best Answer

enter image description here

\documentclass[letterpaper]{article}
\usepackage[linesnumbered,ruled]{algorithm2e}
\begin{document}

\SetAlCapHSkip{0em}
\begin{algorithm}[t]
Initialize 
\\ 
Do stuff ...
\parbox{\linewidth}{\caption{Example caption long enough caption to see how it wraps at page borders.}}
\label{alg:example}
\end{algorithm}

\end{document}

You can add the command \SetAlCapHSkip{<length>} to control that space before the caption. Here, I just reset it to zero by \SetAlCapHSkip{0em}. You can choose whatever value you feel nice.

In case you need to adjust the indentation of the numbers also, you can use the command \DecMargin{<length>} to decrease their margin by the value <length>.

Update

The caption package is not actually necessary here. For your edited part of the question, you can solve the width issue by using a \parbox (of width equal to the total \linewidth) for the caption as follows:

\parbox{\linewidth}{\caption{<...>}}