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

  • 2
    Welcome to TeX.SX! It's not really related to your question, but are you aware of the exam class? It's a great tool to produce exams in LaTeX. – Vincent Feb 23 at 13:28
  • 1
    Do not redefine a command like \marks unless you really know what you are doing! – Ulrike Fischer Feb 23 at 14:00
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.

|improve this answer|||||
  • Thanks, yes this works better with your final example. Changed my accepted answer to this one. – jmmcd Feb 24 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

|improve this answer|||||
  • Thanks, this works, and it works in my real exam too. I had never heard of \null before and after reading tex.stackexchange.com/questions/24919/… I am confused, but it works! – jmmcd Feb 23 at 12:57
  • Sorry, but this is too simplistic. – egreg Feb 23 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.