如何根据实际文本宽度调整 parbox 的大小?

如何根据实际文本宽度调整 parbox 的大小?

语境

我正在开发一个伪代码包,用于编写支持多行的算法。然后,可以拆分长行并相应地处理正确的标识。我使用parboxes 完成这项工作。

如果文本左对齐,看起来会更好,因此使用\RaggedRight(from ),因为连字符也有帮助。ragged2e

问题

代码的每一行都可以有一个注释,注释位于右边距。文本部分的大小会调整以容纳注释。

长注释也可能跨越多行。发生这种情况时,必须限制注释的最大宽度。

不幸的是,当注释超过一行时,它不会触及右边距,因为注释在内部左对齐,parbox并且的大小parbox设置为固定的最大宽度(参见 MWE)。

我的问题

是否有可能使parbox为所容纳文本的实际宽度,因此文本(而不是parbox)触及右边距,同时保持左对齐?

平均能量损失

\algparbox是我处理文本和评论的实现。我为这个 MWE 重写了它的一个更简洁的版本,隐藏了算法细节(例如嵌套控制)。

注释比\largestcommentwidth断行更宽。

\documentclass{article}
\usepackage[english]{babel}

\usepackage[showframe]{geometry} 

%% uncomment to make parboxes visible
%\let\pb\parbox
%\setlength{\fboxsep}{0pt}
%\setlength{\fboxrule}{0.01pt}
%\renewcommand{\parbox}[3][]{\fbox{\pb[#1]{#2}{#3}}} 

\def\commentleftsymbol{$\triangleright$}

\newlength{\commentwidth}
\newlength{\largestcommentwidth}
\setlength{\largestcommentwidth}{3.5cm}
\newlength{\commentsep}
\newlength{\commentsymbollength}


\usepackage{ragged2e}
\newcommand{\algparbox}[3]{%
    % #1: comment
    % #2: text
    % #3: hangindent
    \def\comment{#1}
    \ifx\comment\empty%
        \setlength{\commentsep}{0pt}%
        \setlength{\commentwidth}{0pt}%
        \setlength{\commentsymbollength}{0pt}%
    \else%
        \setlength{\commentsep}{0.25cm}%
        \settowidth{\commentwidth}{\slshape\comment}%
        \settowidth{\commentsymbollength}%
            {\hspace{\commentsep}\commentleftsymbol\hspace{\commentsep}}%
    \fi%
    %
    % HERE: how to adjust \commentwidth to actual text width?
    %
    \ifdim\commentwidth>\largestcommentwidth\relax%
        \setlength{\commentwidth}{\largestcommentwidth}%
    \fi%
    %
    \noindent%
    \parbox[t]%
        {\dimexpr \linewidth - \commentwidth - \commentsymbollength \relax}%
        {\setlength{\hangindent}{#3}\RaggedRight#2\strut}%
    \ifx\comment\empty\else%
        \hspace{\commentsep}\commentleftsymbol\hspace{\commentsep}%
        \parbox[t]{\commentwidth}{\slshape\RaggedRight\comment\strut}%
    \fi%
    \par
}

% short excerpt from kantlipsum text
\newcommand{\kant}{As any dedicated reader can clearly see, the Ideal of practical reason is a representation of, as far as I know, the things in themselves; as I have shown elsewhere, the phenomena should only beused as a canon for our understanding. The paralogisms of practical reason are what first give riseto the architectonic of practical reason.}

\begin{document}
\algparbox{}{Short text without comment}{1cm}
\algparbox{short comment}{Short text with short comment}{1cm}
\algparbox{this is a longer comment}{Short text with long comment}{1cm}\marginpar{$\longleftarrow$~How to avoid this and make the text touch the right margin?}
\algparbox{}{Long text without comment. \kant}{1cm}
\algparbox{short comment}{Long text with short comment. \kant}{1cm}
\algparbox{some little longer comment}{Long text with long comment. \kant}{1cm}\marginpar{$\longleftarrow$~And this?}
\algparbox{longer comment. \kant}{Long text. \kant\ \kant\ \kant}{1cm}\marginpar{Hyphenation helps here, but it's not a solution}
\end{document}

我的包裹algxpar加拿大运输安全局,但这与问题无关。:-)

相关内容