3

I am using this code:

\documentclass{IEEEtran}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{xcolor}
\usepackage[ruled,lined,linesnumbered]{algorithm2e}
\SetKwInput{input}{\textcolor{black}{Input}}
\SetKwInput{output}{\textcolor{black}{Output}}
\renewcommand{\KwSty}[1]{\textnormal{\textcolor{blue}{\bfseries #1}}\unskip}

\begin{document}

\begin{algorithm}
    \input{this text}
    \output{how to write algorithm with \LaTeX2e}
    initialization\;
    \ForEach{\(n \in \mathbb{K}\)}{
        \If{\(n < \tau\)}{
            \(print(\text{"Lower"})\)\;
        }
        \Else{
            \(print(\text{"Greather equal"})\)\;
        }
    }
    \caption{How to write algorithms}
\end{algorithm}

\end{document}

which produces this result:

output

Changing the keyword color also changed the color of the colon (:) after Input/Output. How to set the color of the colon to black? Also, is there a way to make the text of input and output aligned (start at the same position) but without putting extra space before colon in Input?

3
  • The LaTeX compiler reports an "Undefined control sequence" error for \var{n}. Commented Dec 26, 2024 at 18:07
  • I have another solution, I'm still brewing Commented Dec 26, 2024 at 19:10
  • If an answer resolved your issue, kindly mark it as accepted :) Commented Jan 2 at 11:58

2 Answers 2

3

Unfortunately \SetKwInput uses \KwSty internally to set your input/output titles. And, since you've updated \KwSty to include colour, it translates to your \Input.

The solution below implements two items to provide what you're looking for:

  • Updates \SetKwInput (with a patch thanks to etoolbox) to use a newly-defined \InputSty rather than \KwSty to set the input-related content. \InputSty now sets its content using \bfseries.

  • Setting the content within \InputSty using \eqmakebox[<tag>][l]{<stuff>} (thanks to eqparbox. This sets <stuff> in a box of widest width based on the same <tag> (all left-aligned).

enter image description here

\documentclass{IEEEtran}
\usepackage{amsmath,amssymb,amsfonts}
\usepackage{xcolor}
\usepackage[ruled,lined,linesnumbered]{algorithm2e}

\usepackage{etoolbox,eqparbox}
\patchcmd{\SetKwInput}% <cmd>
  {\KwSty}% <search>
  {\InputSty}% <replace>
  {}{}% <success><failure>
\newcommand{\InputSty}[1]{\eqmakebox[algorithm][l]{\bfseries #1}}

\SetKwInput{Input}{\textcolor{black}{Input}}
\SetKwInput{Output}{\textcolor{black}{Output}}
\renewcommand{\KwSty}[1]{\textnormal{\textcolor{blue}{\bfseries #1}}\unskip}
\newcommand{\Print}[1]{\textit{print}(``#1'')}

\begin{document}

\begin{algorithm}
  \Input{this text}
  \Output{how to write algorithm with \LaTeX2e}
  initialization\;
  \ForEach{\(n \in \mathbb{K}\)}{
    \If{\(n < \tau\)}{
      \Print{Lower}\;
    }
    \Else{
      \Print{Greather equal}\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}

\end{document}

Compile at least twice on the first go, or with any change in your \SetKwInput content. One can also make this algorithm-specific if needed.

3

As @Werner explained clearly \SetKwInput uses \KwSty internally to set your input/output titles. And, since you've updated \KwSty to include colour, it translates to your \Input.

This is an alternative solution that doesn't patch \SetKwInput:

  • it defines new commands \Input and \Output with \SetKwHangingKw
  • since \SetKwHangingKw doesn't print : at end of keyword, [but] line is numbering if linesnumbered is set, we need to patch that behaviour if we want to act like \SetKwInput
  • bonus: now you have a new tool at your disposal "\nonl" that let's you disable line numbers for specific lines

output


Full code

\documentclass{IEEEtran}

\usepackage{amsmath, amssymb, amsfonts}
\usepackage{xcolor}% needed for \normalcolor
\usepackage[ruled, lined, linesnumbered]{algorithm2e}
\usepackage{eqparbox}

% Changing the color of keywords
\renewcommand{\KwSty}[1]{\textcolor{blue}{\textbf{#1}}}

% Aligning Input and Output keywords
\SetKwHangingKw{Input}{\eqmakebox[InOutKeywords][l]{\normalcolor Input:}}
\SetKwHangingKw{Output}{\eqmakebox[InOutKeywords][l]{\normalcolor Output:}}

% Disabling line numbers for specific lines with 'linesnumbered' option on
% code taken from https://tex.stackexchange.com/a/153906/127473, thanks Werner ;)
\let\oldnl\nl% Store \nl in \oldnl
\newcommand{\nonl}{\renewcommand{\nl}{\let\nl\oldnl}}% Remove line number for one line

% Patching 'Input' and 'Output' keywords to prepend '\nonl' command
\NewCommandCopy{\oldInput}{\Input}
\NewCommandCopy{\oldOutput}{\Output}
\RenewDocumentCommand{\Input}{m}{{\nonl\oldInput{#1}}}
\RenewDocumentCommand{\Output}{m}{{\nonl\oldOutput{#1}}}

% Sets up utility commands
\NewDocumentCommand{\Print}{ m }{\text{print(``#1'')}}% 
% you can use it in both text and math mode

\begin{document}

\begin{algorithm}
    \Input{this text}
    \Output{how to write algorithm with \LaTeX2e}
    
    \BlankLine
    initialization\;
    
    \BlankLine
    \ForEach{\(n \in \mathbb{K}\)}{
        \eIf{\(n < \tau\)}{
            \Print{Lower}\;
        }{
            \(\Print{Greather equal}\)\;
        }
    }
    
    \caption{How to write algorithms}
\end{algorithm}

\end{document}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.