4

I have a custom underlining command \cleverul{text} proposed by @Jinwen here.

I would like to be able to pass the command inside a redefinition of the \@makechapterhead macro, as a formatting value whose function is to beautifully underline the output of {\@chapapp{} \thechapter} (could also be used for the name of the chapter, i.e. ##1).

The solution that comes to my mind is to create a variant involving a \small-style syntax.

Question is, is there a simple way to convert my underlining command \cleverul{text} into a {\coolclevul text} command?

Or, do you see a way to pass the \cleverul command with its current syntax?

MWE

\documentclass{report}
\usepackage{etoolbox}
\usepackage{contour}
\usepackage{fp}
\usepackage[normalem]{ulem}

% Cool underlining. \cleverul

\contourlength{0.8pt}

\makeatletter

\newcommand{\cleverul}[1]
{%
    \FPmul\cleverul@temp{\f@size}{0.15}%
    \setlength{\ULdepth}{\cleverul@temp pt}%
    \FPdiv\cleverul@temp{\f@size}{15}% 
    \renewcommand{\ULthickness}{\cleverul@temp pt}%
    \uline{\phantom{#1}}%
    \llap{\contour{white}{#1}}%
}%


% Formatting Chapter-word and Chapter title

\NewDocumentCommand{\chapterformat}{m}{%
    \ifdef{\chapter}{%
        \renewcommand{\@makechapterhead}[1]{%
            \vspace*{50\p@}% 
            {\parindent \z@ \raggedright \reset@font
                \ifnum \c@secnumdepth >\m@ne
                {#1 \@chapapp{} \thechapter}% => Want to be able to pass my \cleverul command in formatting {\@chapapp{} \thechapter}.
                \par
                \vskip 20\p@
                \fi
                {\Huge\bfseries ##1} % Title formatting.
                \par\nobreak
                \vskip 180\p@}} 
    }{}
}

\makeatother

\begin{document}

    
    \chapterformat{\fontsize{60}{90}\selectfont\bfseries}  % => Want to be able to pass my \cleverul command.
    
    \chapter{Cool Underlining}
    
        \Huge\bfseries\cleverul{I love \LaTeX.}
    
    
\end{document}

enter image description here


Question Solved

I end up using two different keys, chapterformat and titleformat for standard formatting commands, chapterstyle and titlestyle for commands that involve delimiters around their argument.

Using @Skillmon's expkv-cs package as shown here, creating my own key=value-style custom command was kid stuff. This variant also includes vertical spacing.

\documentclass{report} % The report style has Chapters.

\usepackage{fontspec} % Allows me to switch to a "bold small caps"-compliant font.

\usepackage{etoolbox} % Provides \ifdef LaTeX wrapper.

\usepackage{contour} % Required for the \cleverul command.
\usepackage{fp} % Required for the \cleverul command.
\usepackage[normalem]{ulem} % Required for the \cleverul command.

\usepackage{expkv-cs}

\setmainfont{Times New Roman}

\newcommand{\giant}{\fontsize{72}{108}\selectfont}

% Cool underlining. \cleverul



\makeatletter

\newcommand{\cleverul}[1]
{%
    \contourlength{0.04ex}
    \FPmul\cleverul@temp{\f@size}{0.12}% The higher, the farther from the text. (font size multiplied by x)
    \setlength{\ULdepth}{\cleverul@temp pt}%
    \FPdiv\cleverul@temp{\f@size}{16}% The higher, the thinner. (font size divided by x)
    \renewcommand{\ULthickness}{\cleverul@temp pt}%
    \uline{\phantom{#1}}%
    \llap{\contour{white}{#1}}%
}%



\NewDocumentCommand{\chapterformat}{mmmmmmm}{
    \ifdef{\chapter}{
        \renewcommand{\@makechapterhead}[1]{%
            \vspace*{#5\p@}% Vertical Space before "ChapterX" (50)
            {\parindent \z@ \raggedright \reset@font
                \ifnum \c@secnumdepth >\m@ne
                {#3#1{\@chapapp{}~\thechapter}}
                \par
                \vskip #6\p@ % Vertical Space after "Chapter X" (20)
                \fi
                {#4#2{##1}}
                \par\nobreak
                \vskip #7\p@}} % Vertical Space after Title (180)
    }{}
}

\makeatother

\ekvcSplitAndForward\coolchap\chapterformat % Defining \coolchap as a split version of \chapterformat, using explkv package.
{
    chapstyle = \relax,
    titlestyle = \relax,
    chapformat  = \Huge\bfseries,
    titleformat = \Large,
    befchap    = 50,
    afchap     = 20,
    aftitle    = 180
}

\begin{document}
    

\coolchap{chapstyle=\cleverul,titlestyle=\textsc,chapformat=\giant\bfseries,titleformat=\Huge\bfseries,afchap=40}  
    
    \chapter{Cool Underlining}
    
    \Huge\bfseries\cleverul{I love \LaTeX.}
    
    
\end{document}

enter image description here

1 Answer 1

3

Simple version: just pass the command as argument. The argument to \cleverul itself will be whatever comes directly after #1 in the definition. To delimit this you should add a brace pair in the definition of \chapterformat:

\ifnum \c@secnumdepth >\m@ne
{#1{\@chapapp{} \thechapter}}% => Want to be able to pass my \cleverul command in formatting {\@chapapp{} \thechapter}.
\par
\vskip 20\p@
\fi
% ...
\chapterformat{\fontsize{60}{90}\selectfont\bfseries\cleverul}

More explicit/structured version: pass the command name and construct the macro and the argument using \csname and \endcsname. These commands construct a macro (a c-ontrol s-equence) from a string, and anything in square or curly braces afterwards is used as argument for this macro if the macro takes arguments, and otherwise it is used as a normal brace group in case of curly braces.

With this you can do things like:

\csname cleverul\endcsname{text to underline}

which is interpreted as:

\cleverul{text to underline}

To also be able to add 'regular' formatting commands, as well as to allow only regular formatting commands, you can define the \chapterformat command to take two arguments, one for the regular commands and one for the csname construct. If you don't want to use the csname part then you can pass relax, which would expand to \relax{some text} which results in {some text} as \relax has no effect other than ending the parsing of arguments before the command (see What does \relax do?).

MWE:

\documentclass{report}
\usepackage{etoolbox}
\usepackage{contour}
\usepackage{fp}
\usepackage[normalem]{ulem}

% Cool underlining. \cleverul

\contourlength{0.8pt}

\makeatletter

\newcommand{\cleverul}[1]
{%
    \FPmul\cleverul@temp{\f@size}{0.15}%
    \setlength{\ULdepth}{\cleverul@temp pt}%
    \FPdiv\cleverul@temp{\f@size}{15}% 
    \renewcommand{\ULthickness}{\cleverul@temp pt}%
    \uline{\phantom{#1}}%
    \llap{\contour{white}{#1}}%
}%


% Formatting Chapter-word and Chapter title

\NewDocumentCommand{\chapterformat}{mm}{%
    \ifdef{\chapter}{%
        \renewcommand{\@makechapterhead}[1]{%
            \vspace*{50\p@}% 
            {\parindent \z@ \raggedright \reset@font
                \ifnum \c@secnumdepth >\m@ne
                {#1\csname#2\endcsname{\@chapapp{} \thechapter}}% => Want to be able to pass my \cleverul command in formatting {\@chapapp{} \thechapter}.
                \par
                \vskip 20\p@
                \fi
                {\Huge\bfseries ##1} % Title formatting.
                \par\nobreak
                \vskip 180\p@}} 
    }{}
}

\makeatother

\begin{document}
    \chapterformat{\fontsize{60}{90}\selectfont\bfseries}{cleverul}  % => Want to be able to pass my \cleverul command.
    \chapter{Cool Underlining}
    \Huge\bfseries\cleverul{I love \LaTeX.}
    
    \chapterformat{\fontsize{60}{90}\selectfont\bfseries}{relax}  % => Want to be able to pass my \cleverul command.
    \chapter{Just large and bold}   
\end{document}

Result:

enter image description here

3
  • Thanks, I was not excluding this possibility as a last resort, although the idea was not to pass a command name, but the whole command. I was more on the idea of including the backslash in the value, for cohesion's sake with regular commands. This might sound like nitpicking, but I'm curious about how this might be achieved. Commented Jan 22, 2023 at 21:27
  • 1
    @VincentKrebs it also works to do {#1{\@chapapp{} \thechapter}} and then \chapterformat{\fontsize{60}{90}\selectfont\bfseries\cleverul} (and defining the command to have a single argument again). However, I find the csname version with two arguments cleaner.
    – Marijn
    Commented Jan 22, 2023 at 21:42
  • Yeah, nice little workaround. I was not sure if I could use #1{} to represent a command, and I didn't even try! I think I'll do {#2#1{\@chapapp{} \thechapter}}, #1 being optional and defaulting to \relax. Thanks! Commented Jan 22, 2023 at 22:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .