# Formatting keywords, data in algorithm2e

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}{
\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}

• Would you show us a complete, minimal working example (MWE) of how you intend to use it and also the expected output? You can use \KwData outside the algorithm environment.
– Werner
May 20 '14 at 18:31
• @Werner, what MWE do you need, I am just asking how to reference the keyword outside of algorithm, I mean I haven't got a clue how to do it, I do not even know how to make a MWE. May 20 '14 at 21:57
• What do you mean by "reference"? Typically a reference refers to a "cross-reference" where you use \label...\ref. But I'm not sure whether this is what you're after. So I was asking about what you exactly want in return (say, this text) for this "reference" (if that's what you're after). The Data: is formatted using \KwSty, but this text is formatting as regular text.
– Werner
May 20 '14 at 22:09
• @Werner: the reference I meant is indeed \label....\ref, the keyword data in the algorithm are formatted with special fonts, ultimately, what I want is to use exactly the same fonts in text as they are used in algorithm to maintain consistency. Hope this clarifies May 20 '14 at 22:43

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):

• \ArgSty{<stuff>}

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

• \KwSty{<stuff>}

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

• \FuncSty{<stuff>}

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

• \CommentSty{<stuff>}

Formatting of comments (default is \texttt):

• \TitleSty{<stuff>}

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

• ...

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

\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}{
\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).