7

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.

\documentclass[12pt]{article}
\usepackage{newtxtext}        
\usepackage{newtxmath}      
\usepackage[boxed]{algorithm2e} 

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

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.

1 Answer 1

8

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

\documentclass[12pt]{article}
\usepackage{newtxtext}
\usepackage{newtxmath}
\usepackage[boxed]{algorithm2e}

\begin{document}
\begin{algorithm}
\LinesNumbered
\SetKwInOut{Input}{input}
\SetKwInOut{Output}{output}
\ResetInOut{output}% <- added here
\Input{the input}
\Output{the output}
Do something
\caption{The  algorithm}
\end{algorithm}
\end{document}

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'
\sbox\algocf@inoutbox{\KwSty{#2\algocf@typo:}}%

% 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.

1
  • Any progress on solving this possible bug? Today it is still there. Your solution works locally. Thanks a lot.
    – Sigur
    Dec 23, 2021 at 18:01

You must log in to answer this question.

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