1

I'm using algorithm2e and my lines are breaking too early. I would like to reduce the size of the margin on the right side, without affecting the left margin. I'm interested in doing it, because right now the margins in the caption are asymmetric, which doesn't look good to me.

enter image description here

I tried using \DecMargin but this affects the margin in both sides. It's most important for me to decrease the margin size it in the caption, if that's easier. A solution that can make the caption have equal (small) margins will also be accepted.

Here is an MWE

\documentclass{article}
\usepackage[ruled]{algorithm2e}

\begin{document}

\begin{algorithm}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore 
et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut 
aliquip ex ea commodo consequat.
\caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut 
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut 
aliquip ex ea commodo consequat.}
\end{algorithm}

\end{document} 

Interactive MWE:

https://www.latex4technics.com/?note=92u930

2
  • I am sorry, but I fail to see the interest of such a practice, at least with the provided example. Could you give a bit more context to your question ?
    – BambOo
    Commented May 10, 2020 at 14:33
  • I've updated my question. Does that help?
    – Atnas
    Commented May 12, 2020 at 8:01

1 Answer 1

1

Your question is somewhat unclear because you have seemingly opposite statements in it :

I would like to reduce the size of the margin on the right side, without affecting the left margin.

and

because right now the margins in the caption are asymmetric, which doesn't look good to me.

So I assumed that the most important part was to get symmetric (controllable) caption margins. I made a patch to de definition of \algocf@makecaption@ruled. The actual problem seems to stem from the various \hsize manipulations, though I could not trace the problem back to its source. I therefore decided to discard the hsize call alltogether and to start from its base value which is \linewidth.

See the modified code below.

\documentclass{article}
\usepackage[ruled]{algorithm2e}

% Patch caption box width to have left and right margins as \AlCapHSkip
% Based on definition of \algocf@makecaption@ruled at line 2319 of algorithm2e.sty
\makeatletter
\setlength{\algocf@lcaptionbox}{\linewidth} % reset the caption bow width to be as large as the rules 
\renewcommand{\algocf@makecaption@ruled}[2]{%
\global\sbox\algocf@capbox{\hskip\AlCapHSkip % left indent of the box
\addtolength{\algocf@lcaptionbox}{-2\AlCapHSkip}% reduce box width to account for symmetric margins
\parbox[t]{\algocf@lcaptionbox}{\algocf@captiontext{#1}{#2}}}% print the caption
}%
\makeatother

% Note that with these modifications, the \AlCapHskip is now independent of \algomargin
%\setlength{\algomargin}{1cm}
%\SetAlCapHSkip{2cm}


\begin{document}

\begin{algorithm}
    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
    \caption{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.}
\end{algorithm}

\end{document}

enter image description here

You must log in to answer this question.

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