tcolorbox 的垂直中断

tcolorbox 的垂直中断

我正在使用 tcolorbox 手册 (v 3.91) 中的一个示例来突出显示一些文本。问题是不允许换行。所以我尝试tcolorbox对突出显示的文本的每个单词使用。在我看来,换行将自然运行,因为我们有“独立”的框。我实现了一个基于序列的函数(尚未优化),但我仍然离我想要的还很远。

获得的输出 得到的结果

期望的输出 期望结果

这是 MWE

\documentclass[a4paper,8pt]{article}

\usepackage{fullpage}
\usepackage{expl3,xparse}
\usepackage[all]{tcolorbox}

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~keyword~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\DeclareTotalTCBox{\keyword}{ O{green} m O{} }
 { fontupper=\ttfamily,nobeforeafter,tcbox raise base,arc=0pt,outer arc=0pt,
 top=0pt,bottom=0pt,left=0mm,right=0mm,
 leftrule=0pt,rightrule=0pt,toprule=0.3mm,bottomrule=0.3mm,boxsep=0.5mm,
 colback=#1!10!white,colframe=#1!50!black,colupper=red!75!black,#3}{#2}

\begin{document}
 \ExplSyntaxOn
  \seq_new:N \l_notes_list_seq
  \NewDocumentCommand{\markeyword}{m}
  {%
   \notes_applay:n {#1}
  }%
  \cs_new_protected:Nn \notes_applay:n
  {%
   \seq_set_split:Nnn \l_notes_list_seq { ! } {#1}
   \seq_map_inline:cn { l_notes_list_seq }
   {
    \keyword{##1}
    \keyword{~}
   }
  }%
 \ExplSyntaxOff
 Using several tcolorboxes, the result is\\
 Some text to put the highlighted text at the end of the line. Would it breaks? \keyword{A}\keyword{ }\keyword{friend}\keyword{ }\keyword{in}\keyword{ }\keyword{need}\keyword{ }\keyword{is}\keyword{ }\keyword{a}\keyword{ }\keyword{friend}\keyword{ }\keyword{indeed.}


 \vspace{1cm}
 Using the function, the result is\\
 Some text to put the highlighted text at the end of the line. Would it breaks? \markeyword{A!friend!in!need!is!a!friend!indeed.}


 \vspace{1cm}
 The desired output (manually adjusted)\\
 Some text to put the highlighted text at the end of the line. Would it breaks? \keyword{A friend in need is a}\\ \keyword{friend indeed.}
\end{document}

答案1

您的方框之间没有破发点。添加适当的惩罚:

\documentclass[a4paper,8pt]{article}

\usepackage{fullpage}
\usepackage{expl3,xparse}
\usepackage[many]{tcolorbox}

%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~keyword~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\DeclareTotalTCBox{\keyword}{ O{green} m O{} }
 { fontupper=\ttfamily,nobeforeafter,tcbox raise base,arc=0pt,outer arc=0pt,
 top=0pt,bottom=0pt,left=0mm,right=0mm,
 leftrule=0pt,rightrule=0pt,toprule=0.3mm,bottomrule=0.3mm,boxsep=0.5mm,
 colback=#1!10!white,colframe=#1!50!black,colupper=red!75!black,#3}{#2}

\begin{document}
 \ExplSyntaxOn
  \seq_new:N \l_notes_list_seq
  \NewDocumentCommand{\markeyword}{m}
  {%
   \notes_applay:n {#1}
  }%
  \cs_new_protected:Nn \notes_applay:n
  {%
   \seq_set_split:Nnn \l_notes_list_seq { ! } {#1}
   \seq_map_inline:cn { l_notes_list_seq }
   {
    \keyword{##1}\penalty0
    \keyword{~}\penalty0
   }
  }%
 \ExplSyntaxOff

 Using the function, the result is\\
 Some text to put the highlighted text at the end of the line. Would it breaks? \markeyword{A!friend!in!need!is!a!friend!indeed.}

\end{document}

在此处输入图片描述

相关内容