4

How can I print the values of \labelwidth/\labelsep for both questions and parts?

\documentclass{exam}

\usepackage{verbatim}

\renewcommand{\questionshook}{%
  \setlength{\leftmargin}{0pt}%
  \setlength{\labelwidth}{-\labelsep}%
}

\renewcommand{\partshook}{%
    \setlength{\labelwidth}{8mm}%
    \setlength{\leftmargin}{\labelwidth}%
    \setlength{\labelsep}{0pt}%
    \setlength{\itemsep}{0.5\baselineskip}%
    \def\makelabel##1{##1}%
}

\begin{document}
    The length of question \verb|\labelwidth| is ??? and its \verb|\labelsep| is ???, while the length of part \verb|\labelwidth| is ??? and its \verb|\labelsep| is ???.
\end{document}
Diaa
  • 9,599
  • Use the magic \the: \the\labelwidth, \the\labelsep – Phelype Oleinik Sep 02 '19 at 21:06
  • @PhelypeOleinik How to differentiate between that of question and the other of part? – Diaa Sep 02 '19 at 21:08
  • You cannot, unless you use different names for them. Once you execute \questionshook, \labelwidth will be -\labelsep (whatever value the latter may have), and after you execute \partshook, \labelwidth will be 8mm. It can't be both at the same time. You can, of course, save those values in a different register, say, \partlabelwidth and \questionlabelwidth, and then use these in the document. – Phelype Oleinik Sep 02 '19 at 21:10
  • @PhelypeOleinik I would be grateful if you could edit my MWE to show me how to preserve and print the values of both length registers without overwriting each other. – Diaa Sep 02 '19 at 21:13

1 Answers1

4

You cannot have one length register hold two different values. Once you execute \questionshook, \labelwidth will be -\labelsep (whatever value the latter may have), and after you execute \partshook, \labelwidth will be 8mm. It can't be both at the same time.

What you can do, is save those values in a different register, say, \partlabelwidth and \questionlabelwidth, and then use these names in the document.

In the MWE below I defined four new length registers, \partlabelsep, \partlabelwidth, \questionlabelsep, and \questionlabelwidth. Then, inside a group, I used \partshook to make the part values active, and then copied them to the new registers using \global\setcounter, and then the same to \questionshook. Beware that some values are not initialised in \questionshook, so the order here matters!

When you have those values, you can use the mighty \the to print their value:

\documentclass{exam}
\usepackage{verbatim}
\renewcommand{\questionshook}{%
  \setlength{\leftmargin}{0pt}%
  \setlength{\labelwidth}{-\labelsep}%
}
\renewcommand{\partshook}{%
    \setlength{\labelwidth}{8mm}%
    \setlength{\leftmargin}{\labelwidth}%
    \setlength{\labelsep}{0pt}%
    \setlength{\itemsep}{0.5\baselineskip}%
    \def\makelabel##1{##1}%
}
\newlength{\partlabelsep}
\newlength{\partlabelwidth}
\newlength{\questionlabelsep}
\newlength{\questionlabelwidth}
\begingroup
  \partshook
  \global\setlength{\partlabelsep}{\labelsep}
  \global\setlength{\partlabelwidth}{\labelwidth}
  \questionshook
  \global\setlength{\questionlabelsep}{\labelsep}
  \global\setlength{\questionlabelwidth}{\labelwidth}
\endgroup
\begin{document}
    The length of question \verb|\labelwidth| is \the\questionlabelwidth\ and its \verb|\labelsep| is \the\questionlabelsep,
    while the length of part \verb|\labelwidth| is \the\partlabelwidth\ and its \verb|\labelsep| is \the\partlabelsep.
\end{document}
  • 1
    @Diaa Yes, the way they are defined there they just change the value of some registers. About converting units, there's this question, with a few nice answers. If you don't mind loading expl3 then you can use \dim_to_decimal_in_unit:nn{3.1415pt}{1mm}. If neither of these is good for you, you can define \def\convertunit#1#2{\strip@pt\dimexpr1pt*\number\dimexpr(#1)\relax/\number\dimexpr(1#2)\relax\relax\ #2}, and then \convertunit{3.1415pt}{mm}. \convertunit{\labelsep}{mm} will also work. – Phelype Oleinik Sep 02 '19 at 21:51
  • Many thanks for the help. I deleted my original comment after I found some relevant questions, however, your comment is equally helpful. – Diaa Sep 02 '19 at 22:22
  • 1
    @Diaa No worries. Glad it helped :-) – Phelype Oleinik Sep 02 '19 at 22:25