5

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}
4
  • 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, 2014 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, 2014 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, 2014 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, 2014 at 22:43

1 Answer 1

5

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

You must log in to answer this question.

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