1

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?

6
  • Don't mix the use of algorithm2e with algpseudocode. One or the other.
    – Werner
    Sep 17, 2015 at 20:07
  • Why is it a problem? It does not give me any error and I use it for \Call.
    – Simon
    Sep 17, 2015 at 20:20
  • You can write your own algorithm2e-like \Call. They each handle the display of content differently.
    – Werner
    Sep 17, 2015 at 20:21
  • Ok, did not know that there can be conflicts. Fixed :)
    – Simon
    Sep 17, 2015 at 20:42
  • You need an inner loop without an outer one, this is illogical. I know of no language in which you can work in inner loop skipping the outer one.
    – AboAmmar
    Sep 17, 2015 at 20:57

1 Answer 1

4

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{<...>}}
3
  • Many thanks! But what if cannot use the caption package?
    – Simon
    Sep 17, 2015 at 23:34
  • And I also added a new small question. I would really appreciate if you can help me :)
    – Simon
    Sep 18, 2015 at 0:30
  • @Simon Please see my update.
    – AboAmmar
    Sep 18, 2015 at 1:32

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.