2

I am using Algorithm2E and I find myself in a situation where I want to express while(!condition);, but I cannot do that on a single line, it seems. This is the closest I got:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[linesnumbered, ruled]{algorithm2e}
\begin{document}

\begin{algorithm}[H]
\SetKwFunction{KwCrit}{critcalSection}
\SetKwFunction{Kwtas}{test-and-set}
\While{$\Kwtas(r) = 1$}{ }
\KwCrit()\\
$r \gets 0$\\
\caption{Pseudocode of test-and-set usage}
\end{algorithm}

\end{document}

But this puts a single line containing only end. It expresses what I want, but looks quite dirty. How would I be able to express this on a single line?

1
  • Welcome to TeX.SX. Please provide a "Minimal Working Example" (MWE) that starts with \documentclass, includes all relevant \usepackage commands, ends with \end{document} and compiles without errors, even if it does not produce your desired output. Commented Dec 4, 2017 at 17:10

1 Answer 1

3

For every code construction \<code> that puts its content on multiple lines, there is an accompanying \l<code> that puts the content on a single line. You can also define your own \WHILE (and accompanying \lWHILE) using \SetKwFor{<macro>}{<start cond>}{<end cond>}{<end construct>}

enter image description here

\documentclass{article}

\usepackage{algorithm2e}

\SetKwFor{WHILE}{while (}{)}{}

\begin{document}

\begin{algorithm}[H]
  \SetKwFunction{KwCrit}{criticalSection}
  \SetKwFunction{Kwtas}{test-and-set}
  \lWhile{$\Kwtas{r} = 1$}{}
  \lWHILE{$\Kwtas{r} = 1$}{}
  \KwCrit{}\;
  $r \gets 0$\;
  \caption{An algorithm}
\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.