[Tex/LaTex] Measuring line length, using \hfill to justify text, wrapping and right justifying if space too small

horizontal alignmentlengthsspacing

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!

Best Answer

......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.

Related Question