0

I have a long text in a table which is combined from two commands with a space in between, which has to be underlined. How can I make it wrap while being underlined?

The example below shows the behaviour with \underline, \uline and \ul.

Example:

\documentclass{article}

\usepackage{tabularx}
\usepackage{soul}
\usepackage[normalem]{ulem}

\newcommand{\texta}{{Short text from some command.}}
\newcommand{\textb} {{Long text from some other command. This text should be broken on line end and not overflow.}}

\soulregister{\texta}{0}
\soulregister{\textb}{0}

\begin{document}
\begin{tabularx}{\textwidth}{|l|X|}
    \hline
    nothing & \texta\ \textb \\
    \hline
    underline & \underline{\texta\ \textb} \\ % Overflows
    \hline
    uline & \uline{\texta\ \textb} \\ % Overflows
    \hline
    ul & \ul{\texta\ \textb} \\ % Breaks line, but does not underline. Space is ignored.
    \hline

\end{tabularx}
\end{document}

Example output showing cells overflowing

0

The \ul command from the soul package actually works. But you have two problems in the way you use it.

1) Your text won't be underlined because of the additional level of braces in the definitions of \texta and \textb. Start by removing these braces:

\newcommand{\texta}{Short text from some command.}
\newcommand{\textb}{Long text from some other command. This text should be broken on line end and not overflow.}

2) You should expand the argument of \ul:

\edef\myulcmd{\noexpand\ul{\unexpanded\expandafter{\texta}\space\unexpanded\expandafter{\textb}}}%
\myulcmd
  • Thanks! The second part is nothing I could have come up with. – Adrian Feb 22 at 15:55
  • This version is the safest if \texta and \textb may contain commands which should be protected against expansion. Otherwise, you may simply write \edef\myulcmd{\noexpand\ul{\texta\space\textb}}l\myulcmd or \edef\mytext{{\texta\space\textb}}\expandafter\ul\mytext – Eric Domenjoud Feb 23 at 6:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.