# Using the algorithmicx package If Then Else, when line is to long indentation breaks

Ignore the Spanish in the code. The problem is that I am getting the then clause of the nested if with the wrong indentation because of the line break.

\documentclass[11pt, oneside]{article}
\usepackage{algorithm,algpseudocode}
\usepackage{ifthen}
\usepackage{caption}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage{ifthen}

\newcommand{\yLuego}{\land _L}
\begin{document}
\begin{itemize}
\algrenewtext{EndIf}{\textbf{fi}}

\item $filtrarPorMatch(r,c1,c2) =$
\begin{algorithmic}
\If{\o?(r)}
\State \o
\Else
\If{$c1 \in campos(dameUno(r)) \land c2 \in campos(dameUno(r)) \yLuego dameUno(r)[c1] = dameUno(r)[c2]$}
\State $Ag(dameUno(r), filtrarPorMatch(sinUno(r), c1, c2))$
\Else
\State $filtrarPorMatch(sinUno(r), c1, c2)$
\EndIf
\EndIf
\end{algorithmic}

\end{itemize}
\end{document}


Any tips on how to solve it or do it better? I must add that I'm new to LaTeX and just starting to understand how it works.

Edit: I solved it like this, but I don't think it's the right way:

\If{$c1 \in campos(dameUno(r)) \land c2 \in campos(dameUno(r)) \yLuego \newline \hspace*{2em}dameUno(r)[c1] = dameUno(r)[c2]$}

• Done. I included all the packages in the original document except \geometry{a4paper}  that, for some reason , just prints the line "a4paper" on the document. That does not happen in the original – Santiago Sep 16 at 14:46
• What exactly don't you like about the output of your example? It looks aligned correctly to me. Unrelated to the problem: Use \mathit{variable} for typing variable names in math mode, otherwise the innerword spacing is wrong (see e.g the big space in dameUno). – siracusa Sep 16 at 17:29
• I pasted the wrong code snippet by mistake... It was filtrarPorMatch. Thanks for the tip! – Santiago Sep 16 at 20:42

## 1 Answer

Well, I would suggest to use \newline after all logical breaks in your long line -- means after all \land and \yLuego.

In this question you can find the definition of a new command \parState like:

% see https://tex.stackexchange.com/questions/384017/how-to-wrap-lines-correctly-inside-algorithmic-more-indentation-for-children-l
\makeatletter % <=======================================================
\newcommand{\algmargin}{\the\ALG@thistlm}
\makeatother
\algnewcommand{\parState}[1]{\State%
\parbox[t]{\dimexpr\linewidth-\algmargin}{\strut\hangindent=\algorithmicindent \hangafter=1 #1\strut}}
% <=====================================================================


Adding that code to your preamble you can then use the following code

\If{\parState{$c1 \in campos(dameUno(r)) \land %\newline <========== c2 \in campos(dameUno(r)) \yLuego %\newline dameUno(r)[c1] = dameUno(r)[c2]$}}


to write your longer \If statement. Please see that I commented here the included \newline. Please try first this variant and compile to see the result, then uncomment the \newline commands, compile again and check the changed result. Choose that variant you like more ...

Please see this complete TeX code

\documentclass[11pt, oneside]{article}

\usepackage{showframe} % <==============================================
\usepackage{algorithm,algpseudocode}
\usepackage{ifthen}
\usepackage{caption}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{amssymb}

\newcommand{\yLuego}{\land _L}
% see https://tex.stackexchange.com/questions/384017/how-to-wrap-lines-correctly-inside-algorithmic-more-indentation-for-children-l
\makeatletter % <=======================================================
\newcommand{\algmargin}{\the\ALG@thistlm}
\makeatother
\algnewcommand{\parState}[1]{\State%
\parbox[t]{\dimexpr\linewidth-\algmargin}{\strut\hangindent=\algorithmicindent \hangafter=1 #1\strut}}
% <=====================================================================

\begin{document}
\begin{itemize}
\algrenewtext{EndIf}{\textbf{fi}}

\item $filtrarPorValor (r, c, v) =$
\begin{algorithmic}
\If{\o?(r)}
\State \o
\Else
\If{\parState{$c1 \in campos(dameUno(r)) \land %\newline <========== c2 \in campos(dameUno(r)) \yLuego %\newline dameUno(r)[c1] = dameUno(r)[c2]$}}
\State $Ag(dameUno(r), filtrarPorValor(sinUno(r), c, v))$
\Else
\State $filtrarPorValor(sinUno(r), c, v)$
\EndIf
\EndIf
\end{algorithmic}

\end{itemize}
\end{document}


and its result:

After deleting the % in front of the \newlines you get the changed result:

Please see that I used package showframe` to visualize the typing area and margin. Delete it for your real work, it is only here for testing purposes ...