[Tex/LaTex] Formatting keywords, data in algorithm2e

algorithm2ecross-referencing

I am just wondering if there is a way to reference the keyword data outside the algorithm in algorithm2e package. For example, given the example in algorithm2e, if I want to reference \KwData{this text} in my text outside the algorithm, how should I do it?

\begin{algorithm}[H]
\SetLine
\KwData{this text}
\KwResult{how to write algorithm with \LaTeX2e }
initialization\;
\While{not at end of this document}{
read current\;
\eIf{understand}{
go to next section\;
current section becomes this one\;
}{
go back to the beginning of current section\;
}
}
\caption{How to write algorithms}
\end{algorithm}

Best Answer

In the example you've given, this text is completely unformatted, and it's best to use it as-is in your text. However, components of an algorithm are styled using various macros:

  • \DataSty{<stuff>}

    Used to set data (default is \textsf):

    enter image description here

  • \ArgSty{<stuff>}

    Used to set arguments (to functions; default is \textit):

    enter image description here

  • \KwSty{<stuff>}

    Used to prefix input parameters for an algorithm (default is \textbf):

    enter image description here

  • \FuncSty{<stuff>}

    Formatting of a function name (default is \texttt):

    enter image description here

  • \CommentSty{<stuff>}

    Formatting of comments (default is \texttt):

    enter image description here

  • \TitleSty{<stuff>}

    Used to set the title of the algorithm via \TitleOfAlgo (default is regular text):

    enter image description here

  • ...

I would use the appropriate style in the text as well, like in the following example:

enter image description here

\documentclass{article}
\usepackage{algorithm2e}
\begin{document}

\SetKwData{matrixinput}{some matrix}%
\begin{algorithm}[H]
  \KwIn{\matrixinput}
  \KwResult{how to write algorithm with \LaTeX2e }
  initialization\;
  \While{not at end of this document}{
    read current\;
    \eIf{understand}{
      go to next section\;
      current section becomes this one\;
    }{
      go back to the beginning of current section\;
    }
  }
  \caption{How to write algorithms}
\end{algorithm}

We use \matrixinput as input.
\end{document}

The input \matrixinput is defined outside the algorithm environment in order to make it usable outside of that scope (without your text).