1

I'm writing an exam and using a \marks macro with \strut and \hfill to show the marks available for each question right-aligned, bold and in square brackets. But sometimes it doesn't put it on the right! In the MWE output, 1 and 4 are correct, 2 and 3 are wrong. In the source, 1 and 3 have no space before the \marks, 2 and 4 do.

I'm using \strut thanks to the answer here.

\documentclass[11pt]{article}
\usepackage{fullpage}
\renewcommand{\marks}[1]{\strut\hfill\textbf{[#1]}}

\begin{document}

Sample Exam!

\begin{enumerate}
\item xx3 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx\marks{5}
\item xx3 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx \marks{5}
\item xxx4 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx\marks{5}
\item xxx4 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx \marks{5}


\end{enumerate}
\end{document}

MWE

jmmcd
  • 115

2 Answers2

3

Borrowing from D. E. Knuth himself (look for “Bourbaki” in the index of the TeXbook):

\newcommand{\Marks}[1]{{% note the additional brace
  \unskip % remove a possible space
  \nolinebreak % no break here
  \hfil % fill
  \penalty50 % a possible break point here
  \hspace{0.5em}% minimum separation
  \mbox{}% something that cannot be discarded
  \nolinebreak % no break here
  \hfil % fill
  \textbf{[#1]}% the marks
  \parfillskip=0pt % no fill by \par
  \finalhyphendemerits=0 % a hyphen on the line above the marks is good
  \par % end the paragraph
}}

A line break can be taken at \penalty50, which would remove the following \hspace{0.5em}.

The spaces before the % signs in the code are wanted.

\documentclass[11pt]{article}
\usepackage{fullpage}

\newcommand{\Marks}[1]{{% note the additional brace
  \unskip % remove a possible space
  \nolinebreak % no break here
  \hfil % fill
  \penalty50 % a possible break point here
  \hspace{0.5em}% minimum separation
  \mbox{}% something that cannot be discarded
  \nolinebreak % no break here
  \hfil % fill
  [#1]% the marks
  \parfillskip=0pt % no fill by \par
  \finalhyphendemerits=0 % a hyphen on the line above the marks is good
  \par % end the paragraph
}}

\begin{document}

Sample Exam!

\begin{enumerate}
\item xx3 x xxxxxx xxxx xxx xxxxxxxx xxxxx xxxx xxxxxx xxxxxxx xxx xxxxxxxxx xxxxxxxxxx\Marks{5}
\item xx3 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx \Marks{5}
\item xxx4 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx\Marks{5}
\item xxx4 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx \Marks{5}


\end{enumerate}
\end{document}

Don't redefine commands you don't know about, in this case the primitive command \marks.

enter image description here

Try the solution proposed in the other answer with

\item xx3 x xxxxxx xxxx xxx xxxxxxxx xxxxx xxxx xxxxxx xxxxxxx xxxxx xxxxxxxxx xxxxxxxxxx\Marks{5}

and you'll see the difference.

jmmcd
  • 115
egreg
  • 1,121,712
  • Thanks, yes this works better with your final example. Changed my accepted answer to this one. – jmmcd Feb 24 '20 at 22:08
1

You can introduce an unbreakable space ~ with a preceding \null to form a new line when a single line is not wide enough.

\documentclass[11pt]{article}
\usepackage{fullpage}
\renewcommand{\marks}[1]{\null~\hfill\textbf{[#1]}}

\begin{document}

Sample Exam!

\begin{enumerate}
\item xx3 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx\marks{5}
\item xx3 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx \marks{5}
\item xxx4 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx\marks{5}
\item xxx4 x xxxxxx x x x x xxx x x x x x x xx x x x x x xxxx x x x xx x xxxxxxx xxx x x x x xx \marks{5}    
\end{enumerate}

\end{document}

enter image description here

AboAmmar
  • 46,352
  • 4
  • 58
  • 127
  • Thanks, this works, and it works in my real exam too. I had never heard of \null before and after reading https://tex.stackexchange.com/questions/24919/what-is-null-and-when-do-we-need-to-use-it/24921 I am confused, but it works! – jmmcd Feb 23 '20 at 12:57
  • Sorry, but this is too simplistic. – egreg Feb 23 '20 at 13:38