Algorithmic environment: procedure parameters are too long

algorithmicalgorithmsoverflow

I am using the algorithmic environment and the parameters of my Function/Procedure are too long and they overflow. I am wondering what is the solution? Can I somehow force it to stay in a single line or make it multiline nicely? I am not sure.

\documentclass[12pt]{report}
\usepackage[letterpaper, portrait, margin=1in]{geometry}
\usepackage[utf8]{inputenc}

\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath,amssymb}
\usepackage{amsthm}
\usepackage{mathrsfs}

\usepackage{float}
\usepackage{framed}
\usepackage{blindtext}

\pagenumbering{gobble}

\begin{document}

\begin{algorithm}[htbp]
    \caption{Simple greedy visit sequence evaluator for circular remote attribute grammar}\label{alg:crag-visit-sequence-generator}
\begin{algorithmic}
\Procedure{\texttt{VISIT\_SEQUENCE\_EVAL}}{$AG, \hat{\mathscr{D}},\mathscr{V}, \mathit{Val} = \texttt{dict()}, \mathit{history} = \texttt{dict()}, \mathit{inCycle} = \texttt{false}$}
\State \Return TODO
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

enter image description here

Best Answer

A quick and dirty solution is to use \newline in the arguments and then repeat the text that you want to skip horizontally using \phantom, that allocates the space but does not actually print anything. So the following can be added:

\newline\phantom{\textbf{procedure} \texttt{VISIT\_SEQUENCE\_EVAL}(}

which means: new line, then whatever space would be needed if you would print

procedure VISIT_SEQUENCE_EVAL(

and then the rest of the arguments.

MWE (note that I removed most of the math mode from the arguments because it was not needed):

\documentclass[12pt]{report}
\usepackage[letterpaper, portrait, margin=1in]{geometry}
\usepackage[utf8]{inputenc}

\usepackage{algorithm}
\usepackage{algpseudocode}
\usepackage{amsmath,amssymb}
\usepackage{amsthm}
\usepackage{mathrsfs}

\usepackage{float}
\usepackage{framed}
\usepackage{blindtext}

\pagenumbering{gobble}

\begin{document}

\begin{algorithm}[htbp]
    \caption{Simple greedy visit sequence evaluator for circular remote attribute grammar}\label{alg:crag-visit-sequence-generator}
\begin{algorithmic}
\Procedure{\texttt{VISIT\_SEQUENCE\_EVAL}}{$AG, \hat{\mathscr{D}},\mathscr{V}$, \textit{Val} = \texttt{dict()}, \textit{history} = \texttt{dict()},\newline\phantom{\textbf{procedure} \texttt{VISIT\_SEQUENCE\_EVAL}(}\textit{inCycle} = \texttt{false}}
\State \Return TODO
\EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

Result:

enter image description here

Related Question