5

This MWE is part of an exam. Before each question I put a box in the left margin that includes the question number and the max score.

For this MWE only, I changed the \makebox into a \framebox to emphasize my problem below.

The question itself (the content) is supposed to start right AT the end of the left margin, so right UNDER the start of the preceding paragraph. My two problems:

  1. The box seems to occupy more space than I expect: 1.7cm

  2. The box seems to have some space around it, which I don't want.

Obviously I tried to compensate it with negative \hspace, but in more complex real exams, this compensation takes different values, so you keep on adjusting.

How to have a box sized exactly 1.7cm and not more and how to have no space/glue or whatever between the box and the succeeding text?

\documentclass[12pt,a4paper,notitlepage]{report}
\usepackage[left=25mm,right=25mm,top=15mm,height=252mm,includefoot]{geometry}
\usepackage{showframe}              % temporary, to emphasize my point !
\usepackage{xifthen}                % conditionals, booleans

\setlength{\parindent}{0mm}         % no paragraph indentation

\newcounter{countSubExercises}      % number of sub level exercises: a,b,c,...

\newlength{\boxWidth}             
\setlength{\boxWidth}{1.7cm}

\newcommand{\newSubEx}[2]
%   #1 = no. of score pts
%   #2 = content
{
    \refstepcounter{countSubExercises}  % increment subEx counter
    % box with question nr and max score in the left margin
    \hspace*{-\boxWidth}                        
    \framebox[\boxWidth][r]{{\tiny\sl #1p}\enspace{\bf\alph{countSubExercises}}\enspace}
    #2\\
}

\begin{document}
\chapter*{Exercise title}
A first instructive paragraph\\
\newSubEx{2}{Comment on blah blah ...}
\\
\\
A second instructive paragraph\\
\newSubEx{4}{Calculate the perimeter of ...}
\end{document}
3

Since you seem to want a box 1.7cm wide, I guess that the labels (points and letter) should start at 1.7cm from the left margin. So you need to nest a 1.7cm wide box with left alignment into a zero width box with right alignment.

I also added another box for making the letters aligned, compensating for two digit point scores.

Beware of unprotected spaces in your definitions! And don't use \\ as the normal way to end lines/paragraphs.

\documentclass[12pt,a4paper,notitlepage]{report}
\usepackage[left=25mm,right=25mm,top=15mm,height=252mm,includefoot]{geometry}
\usepackage{showframe}              % temporary, to emphasize my point !
\usepackage{xifthen}                % conditionals, booleans

\setlength{\parindent}{0mm}         % no paragraph indentation

\newcounter{countSubExercises}      % number of sub level exercises: a,b,c,...

\newlength{\boxWidth}             
\setlength{\boxWidth}{1.7cm}

\newcommand{\newSubEx}[2]{%
  %   #1 = no. of score pts
  %   #2 = content
  \par\medskip
  \refstepcounter{countSubExercises}% increment subEx counter
  % box with question nr and max score in the left margin
  \makebox[0pt][r]{%
    \framebox[\boxWidth][l]{% <-------------- change to \makebox
      \makebox[1.2em][l]{\tiny\slshape #1p}%
      \enspace
      \textbf{\alph{countSubExercises}}%
      \enspace
    }%
  }%
  #2%
}

\begin{document}
\chapter*{Exercise title}
A first instructive paragraph

\newSubEx{2}{Comment on blah blah ...}

\bigskip

A second instructive paragraph

\newSubEx{4}{Calculate the perimeter of ...}

\bigskip

A third instructive paragraph

\newSubEx{12}{Calculate the perimeter of ...}
\end{document}

enter image description here

| improve this answer | |
  • 1
    The 'unprotected spaces' seemed to be the main cause. Thanks for the cosmetic improvement on the double digit scores (although rare) – mauricev Feb 7 '15 at 15:46
1

You can do that with \llap:

\documentclass[12pt,a4paper,notitlepage]{report}
\usepackage[left=25mm,right=25mm,top=15mm,height=252mm,includefoot]{geometry}
\usepackage{showframe} % temporary, to emphasize my point !
\usepackage{xifthen} % conditionals, booleans

\setlength{\parindent}{0mm} % no paragraph indentation

\newcounter{countSubExercises} % number of sub level exercises: a,b,c,...

\newlength{\boxWidth}
\setlength{\boxWidth}{1.7cm}

\newcommand{\newSubEx}[2]
% #1 = no. of score pts
% #2 = content
{
    \refstepcounter{countSubExercises} % increment subEx counter
    % box with question nr and max score in the left margin
  \leavevmode
  \llap{\framebox[\boxWidth][r]{{\tiny\sl #1p}\enspace{\bf\alph{countSubExercises}}\enspace}}%
    #2\\
}

\begin{document}

\chapter*{Exercise title}
A first instructive paragraph

\newSubEx{2}{Comment on blah blah ...}
\\
\\
A second instructive paragraph

\newSubEx{4}{Calculate the perimeter of ...}

\end{document} 

enter image description here

| improve this answer | |
  • Oh, no! Not in that position! You were just lucky not to use \newSubEx after a \par. – egreg Feb 4 '15 at 20:54
  • @egreg: I confess I didn't check that point. I modified the code, seems to work fine now. – Bernard Feb 4 '15 at 21:02
0

Enclosing it in \makebox[0pt][r]{..} seems to do the job.

\newcommand{\newSubEx}[2]{\refstepcounter{countSubExercises}%
  \makebox[0pt][r]{\framebox[\boxWidth][r]{{\tiny\sl#1p}\enspace{\bfseries
  \alph{countSubExercises}}\enspace}}#2\\\relax}

In any case, I think there are cleaner options, with less \\.

Mmmm… or you just needed a % like Werner said… I'm not sure I understood the question. In case this doesn't do what you need, tell me, and I will remove the answer.

| improve this answer | |

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.