跨行内联 tcolorbox

跨行内联 tcolorbox

如下例所示,通过 可以很方便地给跨行句子加下划线\ul。是否可以用类似的方法自动编辑soul.sty跨行句子?tcolorbox

在此处输入图片描述

\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}

答案1

这是一个使用选项soulpos使用 tcolorbox。它改编自 muzimuzhi 的邮政在 tcolorbox 的 Github 页面上,所以所有荣誉都归于他们。

\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}

图片

您可以更改选项\mybox以满足您的需要。

相关内容