1

I’m using an example from the tcolorbox manual (v 3.91) to highlight some text. The problem is that line breaking is not allowed. So I tried to use a tcolorbox for each word of the highlighted text. In my thoughts that line breaking will operate naturally since we have ‘independent’ boxes. I implemented a function (not yet optimized) based on sequence but I’m still being far from what I want.

The obtained output obtained result

The desired output desired result

Here is the 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 Answer 1

4

You have no break points between your boxes. Add a suitable penalty:

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

enter image description here

You must log in to answer this question.

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