获取 algorithmicx 中剩余的水平空间

获取 algorithmicx 中剩余的水平空间

如何获取 algorithmicx 中块的正确剩余宽度?我尝试使用\linewidth,但它似乎不包括缩进。

梅威瑟:

\documentclass{article}

\usepackage{algorithmicx}

\makeatletter
\newlength\FunName@Indent

\algblockdefx[FunctionLike]{FunctionLike}{EndFunctionLike}[4]{%
    \def\Fun@Start{\textbf{#1} \textsc{#2}[}%
    \settowidth\FunName@Indent{\Fun@Start}%
    \Fun@Start\parbox[t]{\dimexpr\linewidth-\FunName@Indent}{#3](#4)}
}{}

\makeatother


\begin{document}

\begin{algorithmic}
    \FunctionLike{Fun}{MyFunction}{arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10}{foo}
        \FunctionLike{Rule}{Foo}{arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10}{foo}

        \EndFunctionLike
    \EndFunctionLike
\end{algorithmic}

\end{document}

在此处输入图片描述

如您所见,由于 parbox 的宽度不正确,规则 Foo 出现了换行符。

答案1

似乎\ALG@tlm存储了缩进长度。因此也必须减去它:

\dimexpr\linewidth-\FunName@Indent-\ALG@tlm

结果:

输出

完成 MWE:

\documentclass{article}

\usepackage{algorithmicx}

\makeatletter
\newlength\FunName@Indent

\algblockdefx[FunctionLike]{FunctionLike}{EndFunctionLike}[4]{%
    \def\Fun@Start{\textbf{#1} \textsc{#2}[}%
    \settowidth\FunName@Indent{\Fun@Start}%
    \Fun@Start\parbox[t]{\dimexpr\linewidth-\FunName@Indent-\ALG@tlm}{#3](#4)}
}{}

\makeatother


\begin{document}

\begin{algorithmic}
    \FunctionLike{Fun}{MyFunction}{arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10}{foo}
        \FunctionLike{Rule}{Foo}{arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10}{foo}
            \FunctionLike{Rule}{Foo}{arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10}{foo}
                \FunctionLike{Rule}{Foo}{arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10}{foo}
                    \FunctionLike{Rule}{Foo}{arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10}{foo}
                    \EndFunctionLike
                \EndFunctionLike
            \EndFunctionLike
        \EndFunctionLike
    \EndFunctionLike
\end{algorithmic}

\end{document}

相关内容