3

I'm using ulem package to perform clean underlining as advised here https://alexwlchan.net/2017/10/latex-underlines/

It works like a charm, but the line stays at the same thickness whatever the font size.

As a first try, I want the parameters to change accordingly when the font is settled to HUGE (max size with the moresize package).

However when trying to use it, I get an error message "Undefined Control Sequence".

% !TEX TS-program = lualatex

\documentclass[fontsize=12pt,DIV=calc,oneside]{scrarticle}

\usepackage{contour} \usepackage[normalem]{ulem} \usepackage{moresize}

% Underline

\makeatletter    
\newcommand{\cleverul}[1]
{%
    \ifthenelse{\equal{\f@size}{\pointsize{\normalsize}}}%
    {%
        \setlength{\ULdepth}{1.8pt}%
        \renewcommand{\ULthickness}{0.8pt}%
        \uline{\phantom{#1}}%
        \llap{\contour{white}{#1}}%
    }%
    \ifthenelse{\equal{\f@size}{\pointsize{\HUGE}}}%
    {%
    \setlength{\ULdepth}{3pt}%
    \renewcommand{\ULthickness}{1.5pt}%
    \uline{\phantom{#1}}%
    \llap{\contour{white}{#1}}
    }%
}%
\makeatother

\begin{document}

\cleverul{try}

\end{document}

David Carlisle
  • 757,742
  • 1
    Two quick notes: 1) the command \pointsize is undefined, maybe you forgot to copy its definition from somewhere else; 2) the syntax of \ifthenelse is \ifthenelse{condition}{TRUE CODE}{FALSE CODE}, you've forgotten the FALSE part. – Jinwen Feb 26 '22 at 07:43

3 Answers3

5

You can use a simple calculation instead of detection:

\documentclass[fontsize=12pt,DIV=calc,oneside]{scrarticle}

\usepackage{contour} \usepackage[normalem]{ulem} \usepackage{moresize}

\usepackage{fp}

% Underline

\contourlength{0.8pt} % Specifies the distance between the underline and the character parts it does not want to touch.

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

\begin{document}

\noindent\cleverul{try}

\HUGE

\noindent\cleverul{try}

\end{document}

enter image description here

Works like a charm with URLs in Biblatex, just add the following line :

\DeclareFieldFormat{url}{\cleverul{\url{#1}}} % Without the word URL.

or

\DeclareFieldFormat{url}{\mkbibacro{URL}\addcolon\space\cleverul{\url{#1}}} % With the word URL.

enter image description here

Jinwen
  • 8,518
  • The command won't allow for linebreaks. I asked a separate question here https://tex.stackexchange.com/q/683938/262813. – Vincent Krebs Apr 26 '23 at 18:59
5

You can simply set \ULthickness to a font-dependent length:

enter image description here

\documentclass[fontsize=12pt,DIV=calc,oneside]{scrarticle}
\usepackage{fix-cm}  
\usepackage{contour}
\usepackage[normalem]{ulem}
\usepackage{moresize}

\renewcommand\ULthickness{.04em}

\begin{document}

\raggedright

{\tiny \uline{try \the\dimexpr\ULthickness}}

\uline{try \the\dimexpr\ULthickness}

{\huge \uline{try \the\dimexpr\ULthickness}}

{\HUGE \uline{try \the\dimexpr\ULthickness}}

\end{document}

David Carlisle
  • 757,742
0

Isn't that beautiful.

  \documentclass[fontsize=12pt,DIV=calc,oneside]{scrarticle}
\usepackage{moresize}
\usepackage{contour}
\usepackage[normalem]{ulem}
\usepackage{fp}


% Clever Underline

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

\begin{document}

\noindent\tiny I want to give a \cleverul{try}\\
\scriptsize I want to give a {\cleverul{try}}\\
\footnotesize I want to give a \cleverul{try}\\
\small I want to give a \cleverul{try}\\
\normalsize I want to give a \normalsize\cleverul{try}\\
\large I want to give a \cleverul{try}\\
\Large I want to give a \cleverul{try}\\
\LARGE I want to give \cleverul{try}\\
\Huge I want to give a \cleverul{try}\\
\HUGE I want to give a \cleverul{try}\\

\end{document}

enter image description here