Clever Underlining

contourfontsizemacrosrenewcommandulem

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}

Best Answer

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