# Algorithmicx: controlling newline at the begin of a block using flags

I defined rules to automatically generate latex for algpseudocode from Isabelle. The package algospeudocode works quite well for what I want to do, but I would like to be able to control the break of line preceding new blocks. The mechanism I develop to automatically generate latex issues a conditonal version of the \State, which I call \ConditionalState, in redundant places. This command is defined as follows:

\newif\ifjustaddnewline

\newcommand{\ConditionalState}{%
\else%
\State%
\fi}%


The purpose of \ConditionalState is to avoid line breaks in a row. This is exactly what I want to avoid in this example. The placement of \ConditionalState in the generated latex is automatic. I encoded a set of rules for generating the latex representation of programs described using a BNF formalised in Isabelle. Here is the subset of rules which lead to the issue I want to show:

1. Just after opening the algorithmic block, a \ConditionalState is issued;
2. After opening any programming block, a \ConditionalState is issued;
3. Binary commands, such as sequential composition, come in two flavours:
• The first places a \ConditionalState just after the comma, and recursively translates the left and right branch;
• The second doesn't place a \ConditionalState after the infix symbol, it just recursively translates the branches;
4. Terminal nodes of the grammar are output to latex without additional wrapping;

These rules try to guess the right moment of issuing a ConditionalState. For example, consider that assignment is a terminal, and it is nested in a while. These rules add automatically an extra line for assignments when the while opens. Because assignment itself does not issue a preceding ConditionalState, the user can choose one of the two "flavours" of sequential composition: assign_left ;c assign_right generates a latex snippet where both assignments are in a single line, while assign_left ;n assign_right will generate a snippet where each assignment appears in a different line. The decorations "c" and "n" in the sequential composition (;) are used to distinguish between the two optional formatting preferences. In fact these are aliases for the same operation.

The problem in this approach is that two nested blocks have a empty line between them. This occurs because the first block adds a \ConditionalState just after the begin of its scope, and the next block implicetely adds a new line before printing the name of the block.

The following MWE shows a illustration of a snippet generated by this method, cleaned to remove unnecessary stuff.

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

\begin{document}

\begin{algorithm}\caption{Hello Stack Tex Exhange!!}
\begin{algorithmic}[1]
\ConditionalState
\end{algorithmic}
\end{algorithm}
\end{document}


This latex above (with some cleaning) is automatically generated from the following source:

What I want is a way of controlling the \While block by setting a flag, say, justaddnewline. If justaddnewline is true, then the \While block should not enter a new line before showing the name while. On the other hand, if this flag is false, then the \While block should break the line before printing while. I believe this modification would be in the package algoritmicx.sty, which I found a bit difficult to understand (I am just an average user of latex).

The next picture shows the output I want to obtain, giving the same input as before:

Note that the command ConditionalState between the while loops already ensure that the flag justaddnewline will be true before the start of the new \While block.

Thank you very much! :-)

You can peek ahead and see whether a \While immediately follows a \ConditionalState by using \@ifnextchar:

\documentclass{article}

\usepackage{algorithm,algpseudocode}

\makeatletter
\newcommand{\ConditionalState}{%
% Check to see if \While immediately follows \ConditionalState...
\@ifnextchar\While{}{%
% ... if not...
\else%
\State%
\fi}}%
\makeatother

\begin{document}

\begin{algorithm}\caption{Hello \TeX{} Stack Exhange!}
\begin{algorithmic}[1]