2

I want to include inline code examples such as if i < n then i++ and else i--. I'm using the following LaTeX:

\documentclass{article}
\usepackage{algorithmic}
\usepackage{algorithm2e}
\begin{document}
Inline code examples such as \lIf{i < n}{i++} and \lElse{i--} but
line breaks are being injected.
\end{document}

Which produces:

Inline code examples such as if i < n then i++;

and else i--;

but line breaks are being injected.

Rather than what I would expect:

Inline code examples such as if i < n then i++; and else i--; but line breaks are being injected.

How do I prevent linebreaks?

1 Answer 1

1

One is expected to use all algorithm2e macros inside an algorithm environment to write pseudocode. If you want to use it inline in text, then you need to remove the insertion of \par, which most structural pseudocode statements issue.

Below I defined \llIf and \llElse which remove this line-break issuing via a scope-limiting \let\par\relax:

enter image description here

\documentclass{article}
\usepackage{algorithm2e}

\newcommand{\llIf}[2]{{\let\par\relax\lIf{#1}{#2}}}
\newcommand{\llElse}[1]{{\let\par\relax\lElse{#1}}}

\begin{document}
Inline code examples such as \llIf{$i < n$}{$i{+}{+}$} and \llElse{$i{-}{-}$} without
line breaks being injected.
\end{document}

Both macros lack the formal definition (which includes comments), but those aren't needed in inline usage, I assume.

Finally, don't mix the use of algorithmic with algorithm2e. They are two separate packages and shouldn't be used together.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.