I have a LaTeX document displaying an algorithm and the output is incorrect when I use the package newtxtext with algorithm2e. Here is the minimum working example that shows this problem.


\begin{algorithm} \LinesNumbered \SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}  \Input{the input}  \Output{the output} Do
something  \caption{The  algorithm} \end{algorithm} 

Here is the output: enter image description here

Problem: In the above picture, the : symbol appears on a separated line.

How can I fix this problem? I could obviously remove the package \usepackage{newtxtext}. However, it is a suggested package for Springer books (SVMULT class). Thus, I would prefer to find another solution.


This feels like is a bug to me. Here is a quick fix:


\ResetInOut{output}% <- added here
\Input{the input}
\Output{the output}
Do something
\caption{The  algorithm}

colon position fixed

The problem is caused by a fixed distance of the colon from the left margin. \SetKwInOut is supposed to set this distance according to the longest keyword, but this mechanism somehow failed (I guess newtxtext bold font is really wide). So I used \ResetInOut to reset the position of the colon. These can be found in Section 11.1 of the algorithm2e documentation.

I conjecture that the distance measurement is done with regular font. I am very confident to say this is indeed a bug.

The length used in creating these <keyword> : <contents> is \inoutsize, which is the width of \algocf@inoutbox and is updated if \algocf@inoutbox gets wider. However, I found two different definitions for setting up this box, and they are also different from that of \algocf@inputbox!!!

% In `\SetKwInOut'

% In `\ResetInOut'
\sbox\algocf@inoutbox{\hbox{\KwSty{#1\algocf@typo:}\ }}%

% In `\SetKwInput', different box
\sbox\algocf@inputbox{\hbox{\KwSty{#2\algocf@typo:} }}%

Clearly, there is a <space> missing in the first definition.

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.