# Adjusting perimeter of colorbox to fit text

Consider this MWE:

\documentclass[11pt]{article}
\usepackage[left=1.5cm,top=2.5cm,right=3.5cm,bottom=3.2cm,a4paper]{geometry}
\usepackage{xcolor}
\usepackage{setspace}
\usepackage{blindtext}
\usepackage{microtype}

\setstretch{1.3}
\setlength{\fboxsep}{0.4cm} % sets the perimeter around \colorbox
\newcommand{\titleText}{My really really long title goes here}

\begin{document}

\begingroup\setstretch{2.3}
\noindent \colorbox{black}{\parbox{0.95\linewidth}{\raggedright\textcolor{white}{\Huge \expandafter\MakeUppercase\expandafter{\titleText}}}}
\endgroup

\vspace*{2cm}
\blindtext

\end{document}


How can I adjust the perimeter of the colorbox to 'wrap' around the text like this? I'd still like all the same functionality of automatic line breaks etc.

# UPDATE

I tried using soul but that ran into issues with \textcolor{white}. Based on the recommendation from this answer (https://tex.stackexchange.com/a/48549/187447) I tried using ulem. But still I can't get a black polygon to wrap around text. Any ideas?

\documentclass[11pt]{article}
\usepackage[left=1.5cm,top=2.5cm,right=3.5cm,bottom=3.2cm,a4paper]{geometry}
\usepackage{xcolor}
\usepackage{setspace}
\usepackage{blindtext}
\usepackage{microtype}

\setstretch{1.3}
\setlength{\fboxsep}{0.4cm} % sets the perimeter around \colorbox
\newcommand{\titleText}{My really really long title goes here}

\usepackage[normalem]{ulem} % use normalem to protect \emph
\newcommand\hl{\bgroup\markoverwith
{\textcolor{black}{\rule[-6.5ex]{100pt}{14.5ex}}}\ULon}

\begin{document}

\begingroup\setstretch{2.3}
\noindent \hl{\parbox{0.95\linewidth}{\raggedright\textcolor{white}{\Huge \expandafter\MakeUppercase\expandafter{\titleText}}}}
\endgroup

\vspace*{2cm}
\blindtext

\end{document}

• I'm note sure colorbox can do that, or any of the other box constructions, they are called box and not polygon for a reason. It might be that some of the highlight features of ulem might be useful. – daleif Apr 25 '19 at 15:18
• ... or with highlight from soul, see e.g. tex.stackexchange.com/a/5980/121799. – user121799 Apr 25 '19 at 15:21
• @marmot Do you think a TikZ node is a possibility? – Mike Apr 25 '19 at 15:33
• Probably but it won't be trivial because to TikZ node does not know by default where the text ends. What is wrong with soul? – user121799 Apr 25 '19 at 15:35
• @marmot Just tried using soul. However, it won't let me use textcolor{white} - something to do with this I think - tex.stackexchange.com/questions/48501/… – Mike Apr 25 '19 at 15:44

This is not a complete answer (because there is a small gap) but to show that I do not experience the problems with soul you are describing.

\documentclass[11pt]{article}
\usepackage[left=1.5cm,top=2.5cm,right=3.5cm,bottom=3.2cm,a4paper]{geometry}
\usepackage{xcolor}
\usepackage{setspace}
\usepackage{blindtext}
\usepackage{microtype}
\usepackage{soul}
\setstretch{1.3}
\newcommand{\titleText}{My really really long title goes here}

\begin{document}
\sethlcolor{black}
\begingroup\lineskip -2pt
\noindent\textcolor{white}{\Huge\MakeUppercase{\hl{\titleText}}}
\endgroup

\vspace*{2cm}
\blindtext

\end{document}


\makeatletter
\def\SOUL@hlpreamble{%
\setul{\dp\strutbox}{\dimexpr\ht\strutbox+\dp\strutbox\relax}%
\let\SOUL@stcolor\SOUL@hlcolor
\SOUL@stpreamble
}
\makeatother


I get

• Nice work. I wonder.. if you can adjust the height can you also adjust the width? Cause at the moment the width of the black polygon is too narrow around the edges of the text. – Mike Apr 25 '19 at 16:07
• @Mike I don't. Maybe this post works for you. – user121799 Apr 25 '19 at 16:19
• Would this TikZ solution be a feasible option do you think? tex.stackexchange.com/questions/88977/… – Mike Apr 26 '19 at 10:56