0

I followed the sample of Heiko with very excellent explanation about his approach of my request. Since my main idea is to have as well a subtitle I'm little bit stuck why the ˋsubtitleˋ is not set - or better said: I have no idea how to access it properly for a printout.

\documentclass{article}
\usepackage{keyval}
\usepackage{kvoptions}

\SetupKeyvalOptions{ family=mosquito, prefix=mosquito@, }

\makeatletter \DeclareBoolOption{hideInToC} \DeclareStringOption{subtitle}

\newcommand\sectionLvl[3][]{% \begingroup \setkeys{mosquito}{#1}% \ifcase#2\relax % 0 = chapter \def\tmp@mosquito{\chapter}% \or % 1 = section \ifmosquito@hideInToC \def\tmp@mosquito{\section*}% \else \def\tmp@mosquito{\section}% \fi \else % subsection \def\tmp@mosquito{\subsection}% \fi \expandafter\endgroup \tmp@mosquito{#3}{ {sub: \mosquito@subtitle} } }% \makeatother

\begin{document} \tableofcontents

\bigskip
\hrule

\sectionLvl[subtitle={Test Subtitle}]{1}{Title 1 (shown)}
\sectionLvl[hideInToC]{1}{Title 2 (hideInToC)}
\sectionLvl[subtitle={Test Subtitle2}]{1}{Title 3 (shown)}
\sectionLvl{2}{SubTitle 4 (shown)}

\end{document}

Since I switch from ˋkeyvalˋ to ˋkvoptionsˋ I wonder what are the major differences. I'm aware that the syntax is different but has the one or other benefits in sense of performance (when using via LuaHBTex) or better robustness?

LeO
  • 1,451
  • why do you require from your user to use an additional argument, to remember what the number in argument 2 means, and unusual boolean handling? I would setup an interface like \sectionLvl[subtitle={Test Subtitle},level=chapter,toc=false,number=false]{Title 1 (shown)}. – Ulrike Fischer Jan 18 '21 at 19:42
  • Well ... yeah ... might be an option to think of. Its more that it evolves in a certain direction... I took it from other samples - copy'n paste and now a certain complexitey is reached... I would take the level still as numeric but I appreciate the idea for the toc although I think number is not really required. – LeO Jan 18 '21 at 20:11
  • Just an additional note: some of the parameters are optional and one mandatory. Therefore I don't know if it's useful to change the whole interface. – LeO Jan 18 '21 at 20:25

1 Answers1

1

\setkeys is invoked inside a group to keep the settings local. \tmp@mosquito is expanded by \expandafter\endgroup\tmp@mosquito right before ending the group. \mosquito@subtitle, however, is reset by \endgroup to the empty string. Thus, you need a more sophisticated version. The following code keeps \chapter, \section outside the group, but expands \tmp@mosquito and \mosquito@subtitle before ending the group. The unknown tokens of the title in #3 are protected from the hard expansion of \edef by \unexpanded. It was not clear to me, where the subtitle should go, after the chapter/section title or inside. The example uses the more difficult inside version.

\documentclass{article}
\usepackage{keyval}
\usepackage{kvoptions}

\SetupKeyvalOptions{ family=mosquito, prefix=mosquito@, }

\makeatletter \DeclareBoolOption{hideInToC} \DeclareStringOption{subtitle}

\newcommand\sectionLvl[3][]{% \begingroup \setkeys{mosquito}{#1}% \ifcase#2\relax % 0 = chapter \def\tmp@mosquito{\chapter}% \or % 1 = section \ifmosquito@hideInToC \def\tmp@mosquito{\section*}% \else \def\tmp@mosquito{\section}% \fi \else % subsection \def\tmp@mosquito{\subsection}% \fi \edef\x{% \endgroup \expandafter\noexpand\tmp@mosquito{% \unexpanded{#3}% \ifx\mosquito@subtitle@empty \else \space(sub: \unexpanded\expandafter{\mosquito@subtitle})% \fi }% }% \x }% \makeatother

\begin{document} \tableofcontents

\bigskip
\hrule

\sectionLvl[subtitle={Test Subtitle}]{1}{Title 1 (shown)}
\sectionLvl[hideInToC]{1}{Title 2 (hideInToC)}
\sectionLvl[subtitle={Test Subtitle2}]{1}{Title 3 (shown)}
\sectionLvl{2}{SubTitle 4 (shown)}

\end{document}

Stefan Pinnow
  • 29,535
Heiko Oberdiek
  • 271,626