3

I want to present some information in a format which has a scenario described then dotfill to a right aligned result. If the scenario+result would fill the line, I want it to break to the next line and dotfill the entire line with the result right aligned.

I can achieve this with \makebox, but that introduces unwanted spacing between the box and the dotfill so it isn't a continuous run of dots.

MWE:

\documentclass{scrreprt}

\begin{document}

\begin{description} \item This is some text \dotfill Result \item This is some text which almost fills a line so dotfill does not produce an output \dotfill Result \item This is some text which does fill the line and a break occurs but dotfill doesn't dot \dotfill Result \item I would prefer that if the text fills the line, dotfill fills the next line before the result \makebox[3cm]{\dotfill}\dotfill Result \end{description}

\end{document}

compiled ouput

I've tried various combinations of \unskip and \ignorespaces, but they don't give the desired result. I can \kern-3pt the space away, but that feels hacky. I'd much prefer to generalise a solution to a command I can use instead of dotfill:

\NewDocumentCommand\myDotfill{}{\makebox[3cm]{\dotfill}\MAGICSPACEKILLER\dotfill}

Can anyone advise what I should put in a command to try to kill the unwanted horizontal space?

jw5801
  • 147

2 Answers2

6

enter image description here

a version of dotfill using a skip of minimum size 3cm rather than 0pt, and avoiding being discarded on a line break

enter image description here

\documentclass{scrreprt}
%\ShowCommand\dotfill
\NewDocumentCommand\mdotfill{}{%
\mbox{}\nobreak\cleaders \hbox to .44em{\hss .\hss }%
%\hfill
\hskip 3cm plus 1filll
 \kern 0pt\relax} 
\begin{document}

\begin{description} \item This is some text \dotfill Result \item This is some text which almost fills a line so dotfill does not produce an output \mdotfill Result \item This is some text which does fill the line and a break occurs but dotfill doesn't dot \mdotfill Result \item I would prefer that if the text fills the line, dotfill fills the next line before the result \mdotfill Result \end{description}

\end{document}

David Carlisle
  • 757,742
  • Thanks David! I know I'm not supposed to use the comments for just that, but I wanted to specifically thank you for leaving your working in.

    I can see that you used \ShowCommand to find the code for \dotfill, and replaced \hfill in it with \hskip 3cm plus 1filll which does exactly what I was looking for.

    – jw5801 Jul 06 '23 at 01:10
4

For those instances where you want a full-width line at the end, insert a \parnopar before \dotfill.

enter image description here

\documentclass{scrreprt}

\newcommand{\parnopar}{\parfillskip=0pt\par\parskip=0pt\noindent}

\begin{document}

\begin{description} \item This is some text \dotfill Result \item This is some text which almost fills a line so dotfill does not produce an output \dotfill Result \item This is some text which does fill the line and a break occurs but dotfill doesn't dot \par \dotfill Result \item I would prefer that if the text fills the line, dotfill fills the next line before the result \parnopar \dotfill Result \end{description}

\end{document}

Werner
  • 603,163
  • Thanks Werner - this works if I manually add it after compiling and realising that I need it, but David's answer above gives me a general solution that doesn't require any user intervention. – jw5801 Jul 06 '23 at 01:13