Underline width

4,208

Solution 1

REVISED APPROACH

Here is \dunderline[<offset of line top>]{<thickness>}{<content>}.

EDIT: I took Harald's suggestion to place the macro inside of a group, since \ooalign changes \lineskiplimit. Without the extra group, the underline can run into the content below on the next line. With the extra group, as shown in the MWE, extra vertical space is inserted to prevent overlap.

EDITED to use egreg's suggestion of \sbox0{}, rather than \setbox0=\hbox{}.

\documentclass{article}
\usepackage{lipsum,xcolor}
\newcommand\dunderline[3][-1pt]{{%
  \sbox0{#3}%
  \ooalign{\copy0\cr\rule[\dimexpr#1-#2\relax]{\wd0}{#2}}}}
\begin{document}
\dunderline{1pt}{This is a test}

\dunderline{2pt}{This is a test}

\dunderline[-.5pt]{2pt}{This is a test}

\dunderline[-6pt]{2pt}{This is a test} \lipsum[1]
\end{document}

enter image description here

ORIGINAL OFFERING

The magnification is arbitrary, but only between 1x and 2x the original thickness. It performs the result with a double underline. Since an underline is .4pt thick, the 1st parameter offset can only vary between 0pt (1x mag) to .4pt (2x mag).

\documentclass{article}
\newcommand\dunderline[2][.4pt]{%
  \raisebox{-#1}{\underline{\raisebox{#1}{\smash{\underline{#2}}}}}}
\begin{document}
\underline{This is a normal thickness}

\dunderline[.2pt]{This is 1.5x thickness}

\dunderline{This is 2x thickness}

\end{document}

enter image description here

Solution 2

Of course, soul allows line breaks in underlined material. If you want it, then use soul. But you should avoid underlining altogether.

Otherwise, here's a version that is customizable like for \setul.

\documentclass{article}

\newlength{\myuldp}
\newlength{\myulthickness}
\newcommand{\setmyul}[2]{%
  \if\relax\detokenize{#1}\relax\else
    \setlength{\myuldp}{#1}%
  \fi
  \setlength{\myulthickness}{#2}%
}
\newcommand{\setmyuldepth}[1]{%
  \settodepth{\myuldp}{%
    \if\relax\detokenize{#1}\relax\myulalphabet\else#1\fi
  }%
  \addtolength{\myuldp}{0.9pt}% clearance
}
\newcommand{\myulalphabet}{abcdefghijklmnopqrstuvwxyz}
\newcommand{\myulline}{%
  \leaders\hrule
    height -\myuldp
    depth \dimexpr\myuldp+\myulthickness
  \hfil
}
% initialize
\setmyul{0pt}{0.4pt}
\setmyuldepth{}
%%%

\newcommand{\myul}[1]{{\ooalign{#1\cr\myulline\cr}}}


\begin{document}

\myul{Abcdefghi} \myul{abcyj}

\setmyul{}{2pt}

\myul{Abcdefghi} \myul{abcyj}

\setmyul{1pt}{0.4pt}

\myul{Abcdefghi} \myul{abcyj}

\end{document}

An empty argument in \setmyuldepth uses the whole lowercase alphabet to decide the depth; an empty first argument to \setmyul means “don't change the current value”.

enter image description here

Share:
4,208

Related videos on Youtube

Astrolabe
Author by

Astrolabe

Intrested in GR.

Updated on September 27, 2020

Comments

  • Astrolabe
    Astrolabe about 3 years

    It seems, by using the command \setul{}{ pt} in the soul package, we can change the underline width. But I don't want to use this package. Is there any other command for changing the underline width to an arbitrary size, without using any specific package?

    • Steven B. Segletes
      Steven B. Segletes over 5 years
      Are you wishing to use the native \underline macro, or are you open to other solutions that don't use packages? Does it need to line break? If yes, does it need to hyphenate?
    • Astrolabe
      Astrolabe over 5 years
      @StevenB.Segletes: Although I prefer to use '\underline' macro, but other suggestions are welcomed. No it dose not need any line break.
  • Harald Hanche-Olsen
    Harald Hanche-Olsen over 5 years
    Should you wrap the \ooalign in a group, perhaps? It sets \lineskiplimit=-\maxdimen, after all.
  • Steven B. Segletes
    Steven B. Segletes over 5 years
    @HaraldHanche-Olsen I didn't know that, so thank you, I will edit. Without it, the dunderline can interfere with what is below.
  • Karthik Raman
    Karthik Raman about 3 years
    Is there a way to reduce the breadth of the underline? Suppose I wanted to underline only 90% of the text starting from middle of A to the middle of j? (Weird question, but I have a use for it in another script :) )
  • egreg
    egreg about 3 years
    @KarthikRaman I don't think so (at least with soul). Please, add a question with the needed details.
  • Karthik Raman
    Karthik Raman about 3 years
    This worked great for me: \newcommand\ul[1]{% \raisebox{-3pt}{\hspace{0.25ex}\underline{\hspace{-0.25ex}\r‌​aisebox{3pt}{\smash{‌​{#1}}}\hspace{-0.25e‌​x}}}\hspace{0.25ex}}‌​!
  • egreg
    egreg about 3 years
    I'd recommend \sbox0{...} rather than \setbox0=\hbox{...}; the former is color safe, the latter isn't.
  • Sam OT
    Sam OT almost 3 years
    This is a nice command! I'm looking to use it in section titles. Is there any way of making a switch? Compare \textbf{...} with \bfseries. I'd be looking for something like \dulseries{2pt} to switch to using \dunderline{2pt}{...}. I don't know how this would work with stuff like linebreaks, though...
  • Steven B. Segletes
    Steven B. Segletes almost 3 years
    @SamOT If you can define \dunderline to take only a single argument, such as \newcommand\dunderline[1]{{\sbox0{#1}\ooalign{\copy0\cr\rule‌​[\dimexpr-2pt\relax]‌​{\wd0}{1pt}}}}, then you might get away with using the sectsty package and declaring \sectionfont{\dunderline}.
  • Sam OT
    Sam OT almost 3 years
    Great! Thanks for the quick response on an old question :-) I've been trying to use titlesec (for separate reasons) and I have something working with soul+\setul{}{2pt}, but it's not perfect. If I need to improve I'll bear your suggestion in mind!