1

Is there any way to access the current value of a KOMAoption? In my case the option is set using a calculation. I need the result later to figure out where to place something on the page, and it seems silly to perform the calculation twice (especially since the same expression won't work in a macro, where I need it).

\documentclass[a4paper,oneside]{article}

\usepackage[margin=2cm,marginparwidth=4cm,includemp,reversemp]{geometry}

\usepackage{scrlayer-scrpage} \KOMAoptions{headwidth=textwithmarginpar:-\dimexpr\marginparsep+\marginparwidth\relax,footwidth=textwithmarginpar:-\dimexpr\marginparsep+\marginparwidth\relax}

\begin{document}

DEBUG leftmargin=\the\leftmargin

DEBUG marginparwidth=\the\marginparwidth

DEBUG marginparsep=\the\marginparsep

DEBUG textwidth=\the\textwidth

DEBUG textwithmarginpar=??? % \the\textwithmarginpar gives Undefined control sequence.

DEBUG headwidth=??? % \the\headwidth gives Undefined control sequence.

\end{document}

The same question is asked in Read value of \KOMAoption for debugging purposes, but there is no answer to the actual question, only suggestions for alternative ways to debug problems.

The same question is asked in How to use the current value of a KOMA option?, but the only answer given is specific to the fontsize.

Of course I'm interested in an alternative solution to my problem (finding the headwidth), but it would also benefit future visitors to know if there's a general way to access the value of a KOMAoption.

mhwombat
  • 467

1 Answers1

1

First of all note, that option headwidth is not a single value but is an option with three values width:offset:offset as the manuals explain in Section 5.5. So asking for the value of the option will also show the three values.

KOMA-Script provides an experimental interface to ask for the current value of an option. The interface is currently not described in the manual but only the KOMA-Script homepage → Abfrage der gesetzten Optionen. Because this description is in German only, I've asked the KOMA-Script author to describe it at least in scrkernel-basics.pdf in English.

Command \KOMAoptionOf[<prefix command>]{<filename of package or class>}{<option>} can be used to ask for the current value(s) of an option. Because several KOMA-Script options provide more than one value, the result can not only be one value, but also a comma-separated list of values. The command is not expandable, so to use the value as argument of another command, the <prefix command> is used. Even for printing the value this can be needed. However this only shows the value of the option itself, so:

\documentclass{scrartcl}
\usepackage{scrlayer-scrpage}
\KOMAoptions{headwidth=textwithmarginpar:-\dimexpr\marginparsep+\marginparwidth\relax,footwidth=head}
\usepackage{url}
\begin{document}
DEBUG headwidth=\KOMAoptionOf[\url]{scrlayer-scrpage.sty}{headwidth}
\end{document}

just result in:

value of option headwidth = {textwithmarginpar:-\dimexpr\marginparsep+\marginparwidth\relax:}

which is absolutely correct and indeed is the current value of option headwidth. However, it is not the length of the page head. So you cannot use it as a value of another length.

Also your usage of \leftmargin is wrong. \leftmargin is a LaTeX length used for lists. The current left margin of odd pages (or all pages in single sided documents) is \dimexpr \oddsidemargin+1in\relax. The current left margin of even pages is \dimexpr \evensidemargin+1in\relax.

And textwithmarginpar even isn't an option and far away from being a real length. It is a symbolic value to an option, nothing else but a string with a special semantic. However you can easily calculate it yourself. To show the value, you can use \the, even without using KOMA-Script:

\documentclass{article}
\begin{document}
DEBUG textwithmarginpar=\the\dimexpr\textwidth+\marginparwidth+\marginparsep\relax
\end{document}

So your whole question is based on false assumptions.

However, the current width of the page header indeed is an internal value of scrlayer-scrpage. Using an internal value is always a hack and could break whenever the internals change:

\documentclass{scrartcl}
\usepackage{scrlayer-scrpage}
\KOMAoptions{headwidth=textwithmarginpar:-\dimexpr\marginparsep+\marginparwidth\relax,footwidth=head}
\usepackage{url}
\begin{document}
DEBUG headwidth=\csname sls@headwidth\endcsname% WARNING: USING AN INTERNAL MACRO COULD BREAK!!!
\end{document}

would currently result in

Value of the width of the page head = 490.84645pt

A suggestion without using internals would be to calculate it yourself:

\documentclass{scrartcl}
\usepackage{scrlayer-scrpage}
\newlength{\myheadwidth}
\setlength{\myheadwidth}{\dimexpr\textwidth+\marginparsep+\marginparwidth\relax}
\KOMAoptions{headwidth=\myheadwidth:-\dimexpr\marginparsep+\marginparwidth\relax,footwidth=head}
\usepackage{url}
\begin{document}
DEBUG headwidth=\the\myheadwidth
\end{document}

The result is the same as before.

cabohah
  • 11,455