# Measuring line length, using \hfill to justify text, wrapping and right justifying if space too small

I am a lecturer and have an issue when compiling test and exam questions. The questions will be in an enumerate environment and look something like this, where the marks shown in brackets are right justified on the last line of the problem:

1. Determine the reactions and draw the shear force and bending moment diagrams. Hence determine the maximum bending stress experienced by the component. \hfill(10 marks)

Using \hfill works fine when there is sufficient space on the line. In the case where there is insufficient space on the line to accommodate the marks bracket, the text wraps to the start of the next line. In order to right justify it on the next line I use the rather crude code \\ \hfill\makebox{\hspace{13.3cm}(10 marks)} to get the mark bracket right justified on the succeeding line, but have to play with the hspace length to get it to look right.

Is there a way to measure the remaining space on a line and simultaneously measure the length of the marks bracket, then, in the case where the marks bracket length is larger than the remaining space get the marks bracket to right justify on the succeeding line? Note that the exact length of the marks bracket varies (consider the lengths of "(1 mark)" and "(20 marks)" and the exact text of the last line is not known until the text is compiled.

Thanks in anticipation!

• david carlisle has given a good latex answer. the purpose of this comment is to say that the same situation applies when placing a "qed box" (or "tombstone") at the end of the proof of a theorem; this is used in all the theorem packages i'm familiar with. – barbara beeton Aug 28 '13 at 13:03

......component.\hspace*{1em plus 1\fill}(10~marks)


Using the * form stops the space being lost at a line break. Don't put a space character before or after the \hspace as you will introduce additional space. The ~ stops the line breaking between the number and marks.

\documentclass[12pt,a4paper]{article}
\usepackage{calc}
\newlength\marklength
\newcommand\showmarks[1]{ %
\setlength\marklength{\widthof{(#1~marks)}}%
\makebox[\the\marklength]{}%
\hfill%
\llap{(#1~marks)}%
}
\begin{document}
\begin{enumerate}
\item Determine the reactions and draw the shear force and bending moment
diagrams. Hence determine the maximum bending stress experienced by the
component. \showmarks{10}

\item Determine reactions; draw the shear force \& bending moment
diagrams. determine maximum bending stress experienced by
component.\showmarks{10}

\item Determine reactions; draw the shear force \& bending moment
diagrams. determine maximum bending experienced by
component.\showmarks{10}
\end{enumerate}
\end{document}