8

As shown in the following example, underlining a cross-line sentence can be easily done by \ul via soul.sty. Can a cross-line sentence be tcolorboxed automatically in a similar way?

enter image description here

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{soul}
\usepackage{tcolorbox}
\usepackage{parskip}
\begin{document}
In order to show the expected result, allow me to add a few words here.
\ul{This is a cross-line long sentence.} Underlining a cross-line sentence is easy.

In order to show the expected result, allow me to add a few words here.
\begin{tcolorbox}[colback=red!5!white, colframe=red!75!black, right=0pt,
    rightrule=0pt, on line, hbox, sharp corners=east]
    This is a cross-line
\end{tcolorbox}
\begin{tcolorbox}[colback=red!5!white, colframe=red!75!black, left=0pt,
    leftrule=0pt, on line, hbox, sharp corners=west]
    long sentence.
\end{tcolorbox}
Can a cross-line sentence be automatically tcolorboxed this way?
\end{document}
6

1 Answer 1

1

Here is an option using soulpos with tcolorbox. It was adapted from muzimuzhi's post on tcolorbox's Github page, so all credit goes to them.

\documentclass{article}
\usepackage[showframe]{geometry}
\usepackage{soulpos}
\usepackage{tcolorbox}
\usepackage{multicol,lipsum}

\colorlet{ul}{red}
\newtcbox{\mybox}[1][]{
  on line,
  arc=1pt, outer arc=2pt,
  colback=ul!5!white, colframe=ul!75!black,
  boxsep=0pt, left=1pt, right=-0.5pt, top=2pt, bottom=1pt,
  boxrule=0pt, toprule=1pt, bottomrule=1pt, #1
}

\makeatletter
\newcommand\myul[1][red]{%
  \UseName{str_if_eq:nnF}{#1}{red}{\colorlet{ul}{#1}}%
  \myul@}

\ulposdef\myul@[xoffset-start=1pt]{%
    \ifulstarttype{0}%
        {\tcbset{ULsiderule/.append style={leftrule=1pt}}}%
        {\tcbset{ULsiderule/.append style={leftrule=0pt,sharp corners=west}}}%
    \ifulendtype{0}%
        {\tcbset{ULsiderule/.append style={rightrule=1pt}}}%
        {\tcbset{ULsiderule/.append style={rightrule=0pt,sharp corners=east}}}%
    \mybox[ULsiderule]{\vphantom{Ap}\rule{\ulwidth}{0pt}}%
  }
\makeatother

\begin{document}

In order to show the expected result, allow me to add a few words here. \ul{This is a cross-line long sentence.} Can a cross-line sentence be automatically tcolorboxed this way?

In order to show the expected result, allow me to add a few words here. \myul{This is a cross-line long sentence.} Can a cross-line sentence be automatically tcolorboxed this way?

\begin{multicols}{2}
  Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
  eiusmod tempor incididunt ut labore et dolore magna aliqua.
  \newcolumn
  
  Lorem ipsum dolor sit amet, consectetur \myul{adipiscing elit, sed do
  eiusmod tempor incididunt ut labore} et dolore
  \myul[green]{magna aliqua.}
\end{multicols}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc facilisis vitae massa nec cursus. Fusce faucibus gravida dolor. Cras egestas eget nulla ut consequat. Nulla eu mi at mauris feugiat ornare. \myul[orange]{Etiam a efficitur sem, et scelerisque massa. Sed ipsum justo, posuere vitae leo vitae, fermentum dictum lacus.} Morbi et ipsum vel neque congue elementum. In dolor orci, hendrerit vitae sagittis sit amet, pellentesque a magna.
\end{document}

pic

You can change the options in \mybox to suit your needs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .