1

I want underlined text (or math) where the color of the underline is different from the color of the text (e.g. black text underlined by a red line). Therefore I tried the following code using the \uline from ulem-package:

\documentclass{article}
\usepackage{ulem}
\usepackage{xcolor}

\begin{document} \color{red}\uline{\begingroup\color{black} a ä $a$ öü a a a aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a a a aa a\endgroup}\color{black} gsgfsd \end{document}

This produces an error message that \endgroup is missing :(

Why?

How can I fix this?

Jakob
  • 993
  • 1
    Does this help? https://tex.stackexchange.com/questions/321217/underline-text-change-underline-colors-in-consecutive-sentences – Alan Xiang Aug 05 '20 at 15:56
  • Kind of, but I have heard that the soul package has problems with non-English languages. I would prefere ulem – Jakob Aug 05 '20 at 16:06
  • 1
    The documentation of ulem indicates that you can define the following macro for red underline (maybe also need to load xcolor): \newcommand\reduline{\bgroup\markoverwith{\textcolor{red}{\rule[-0.5ex]{2pt}{0.4pt}}}\ULon}. I can do \reduline{\color{blue}abcde}, which gives blue text with red underline. – Alan Xiang Aug 05 '20 at 16:10
  • If you use the same example text that I provided "a a ..." only the first letter is blue, the rest is black. But besides that this works quite well even though I don't understand your code – Jakob Aug 05 '20 at 16:29

1 Answers1

1

If you really want to underline text (in red?), you can use soul

\documentclass{article}
\usepackage{soul}
\usepackage{xcolor}

\newcommand{\redul}[1]{% \begingroup \setulcolor{red}% \ul{#1}% \endgroup }

\begin{document} \redul{a a $a$ aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a aa a a a a a aa a} gsgfsd \end{document}

enter image description here

egreg
  • 1,121,712
  • I would heavily prefere a solution bases on \color{red} instead of \setulcolor{red}, because the \color{red} is the result of another command in my actual project. Is it true that soul has problems with German Umlaute äöü? Edit: Yes, When I plug in äöü in your MWE, I get an error :( – Jakob Aug 05 '20 at 17:22
  • 1
    @Jakob Use soulutf8 – egreg Aug 05 '20 at 17:33
  • Your solution does not work inside math? $a b c \redul{d e f}$ gives an error to me :( – Jakob Aug 05 '20 at 17:48
  • @Jakob Should it? Did you specify that you want to use the command like that? No. You're keeping secret your “another command in my actual project” and give no detail of how you'd like the command to be used and why you want to use it. We can answer what you ask, not read your mind. I'll remove my answer in a few minutes. – egreg Aug 05 '20 at 17:54
  • No need to delete it, maybe other users find it helpful. I always like my commands to be as robust and general as possible. At the moment I am creating the grading scheme for an exam, and I am defining commands that make it easier to indicate which text passages or which parts of a formula gives how many points such that alternative solutions correspond to different colors. I made already hundreds of lines of codes for this purpose, but I tried to make the *M*WE as simple as possible. I will think about how to update the question. – Jakob Aug 05 '20 at 22:19