0

I would like to improve my solution to the following goal. A tcolorbox environment with a title that is partially highlighted. The goal

Thanks to the great answers here and the awesome tcolorbox docs. I achieved this solution, it's so hacky though, the highlighting part is what I don't like:

  1. It has repeated code (as the section title),
  2. The lengths are experimental, I would like it to go from the edge of the page to 2 or 3 letters in.
  3. Use of \vphantom doesn't seem necessary if the highlighting is somehow linked to the title.
\usepackage[a4paper, bottom=1cm, top=1cm, right = 0.5cm, left = 0.5cm]{geometry}
\usepackage[skins,breakable]{tcolorbox}

\newcommand{\sectionTitle}[1]{
% begin bad highlighting code
  \node[inner sep=5pt,text width=0.21\textwidth,
      align=none, rectangle, fill=yellow,
      below right, font=\LARGE]
      at ([xshift=-0.5\textwidth]frame.north west)
  {%
    \vphantom{THI}
  };
% end bad highlighting code
  \node[inner sep=5pt,outer sep=0pt,text width=0.25\textwidth,
      align=none,
      below right, font=\LARGE]
      at ([xshift=-0.3\textwidth]frame.north west) (SECTIONTITLE)
  {%
    #1
  };
}
\newtcolorbox{mycustombox}[1]{
  blanker,
  width=0.6\textwidth,enlarge left by=0.3\textwidth,
  before skip=6pt,
  breakable,
  overlay unbroken and first={%
    \sectionTitle{#1}
},
  overlay unbroken and last={%
    \sectionTitle{#1}
  }
}
\begin{document}
\begin{mycustombox}{My Section Title}
  Content of this section
\end{mycustombox}
\end{document}
2
  • Ok. You don't like the highlighting section but why? what do you like it?
    – Ignasi
    Sep 16 at 7:29
  • I made edits that explain why i don't like the highlighting part.
    – abdulhaq-e
    Sep 16 at 14:38

1 Answer 1

1

I don't think that there is too much hacky or redundant code in your original approach. Also, I don't quite understand what you want to achieve, so I am unsure whether this better suits your needs.

I changed the following:

  • The xshift of the yellow background now shifts it exactly to the left page border.
  • The contents of the yellow node are irrelevant if you want to have a fixed width, so I changed it to \strut. In case the width should depend on the text of the title, see below for an alternative solution.
  • I used the anchor option to place the nodes and removed all unnecessary options.
  • I merged the options overlay unbroken and first and overlay unbroken and last to just overlay because this essentially does the same.

Full code:

\documentclass{article}
\usepackage[a4paper, bottom=1cm, top=1cm, right=0.5cm, left=0.5cm]{geometry}
\usepackage[skins, breakable]{tcolorbox}

\newcommand{\sectionTitle}[1]{
  \node[inner sep=5pt, anchor=north west, font=\LARGE, 
    text depth=0pt, fill=yellow, text width=1.5cm]
    at ([xshift={-0.3\textwidth-\evensidemargin-1in}]frame.north west) {\strut};
  \node[inner sep=5pt, anchor=north west, font=\LARGE]
    at ([xshift={-0.3\textwidth}]frame.north west) {#1};
}
\newtcolorbox{mycustombox}[1]{
  blanker,
  width=0.6\textwidth,
  enlarge left by=0.3\textwidth,
  before skip=6pt,
  breakable,
  overlay={
    \sectionTitle{#1}
  },
}

\begin{document}

\begin{mycustombox}{My Section Title}
  Content of this section
\end{mycustombox}

\end{document}

enter image description here


If you want to have the yellow highlighting to end exactly after the third character of the section title, you could do the following:

\documentclass{article}
\usepackage[a4paper, bottom=1cm, top=1cm, right=0.5cm, left=0.5cm]{geometry}
\usepackage[skins, breakable]{tcolorbox}

\ExplSyntaxOn
\NewDocumentCommand { \getfirstletters } { m m } {
    \str_use:N \str_range:Nnn { #1 } { 1 } { #2 } 
}
\ExplSyntaxOff

\newcommand{\sectionTitle}[1]{
  \node[inner sep=5pt, anchor=north west, font=\LARGE, 
    text depth=0.5ex, fill=yellow, 
    execute at begin node=\hspace*{\dimexpr\evensidemargin+1in-5pt\relax}]
    at ([xshift={-0.3\textwidth-\evensidemargin-1in}]frame.north west) {\phantom{\getfirstletters{#1}{3}}};
  \node[inner sep=5pt, anchor=north west, font=\LARGE]
    at ([xshift={-0.3\textwidth}]frame.north west) {#1};
}
\newtcolorbox{mycustombox}[1]{
  blanker,
  width=0.6\textwidth,
  enlarge left by=0.3\textwidth,
  before skip=6pt,
  breakable,
  overlay={
    \sectionTitle{#1}
  },
}

\begin{document}

\begin{mycustombox}{My Section Title}
  Content of this section
\end{mycustombox}

\bigskip

\begin{mycustombox}{WWWW}
  Content of this section
\end{mycustombox}

\end{document}

enter image description here

I created a little helper macro that allows you to get the first three (or any other number of) letters of a string. To get the first three letters of "Hello", use \getfirstletters{Hello}{3}.

2
  • Awesome, I thought I might be able to combine the highlighting and the title somehow. But this is great since I got to know at ([xshift={-0.3\textwidth-\evensidemargin-1in}]frame.north west) {\strut};
    – abdulhaq-e
    Sep 21 at 13:58
  • 1
    @abdulhaq-e you somehow need to draw the yellow highlighting separately, either using a \node or a \fill command. At least, this is much simpler than trying to have the node that contains the title a colored background. Sep 21 at 14:00

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.