1

Consider this MWE:

enter image description here

\documentclass[11pt]{article}
\usepackage[left=1.5cm,top=2.5cm,right=3.5cm,bottom=3.2cm,a4paper]{geometry}
\usepackage{xcolor}
\usepackage{adjustbox}
\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.

enter image description here

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?

enter image description here

\documentclass[11pt]{article}
\usepackage[left=1.5cm,top=2.5cm,right=3.5cm,bottom=3.2cm,a4paper]{geometry}
\usepackage{xcolor}
\usepackage{adjustbox}
\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}
Mike
  • 416
  • 2
    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
  • 1
    ... or with highlight from soul, see e.g. https://tex.stackexchange.com/a/5980/121799. –  Apr 25 '19 at 15:21
  • @marmot Do you think a TikZ node is a possibility? – Mike Apr 25 '19 at 15:33
  • 2
    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? –  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 - https://tex.stackexchange.com/questions/48501/soul-broken-highlighting-with-xcolor-when-using-selectcolormodel – Mike Apr 25 '19 at 15:44
  • If I add \usepackage{soul} and add \sethlcolor{black} \begingroup\lineskip -2pt \noindent\textcolor{white}{\Huge\MakeUppercase{\hl{\titleText}}} \endgroup then I get something that looks good except for a small gap which I thought to cure with \lineskip -2pt but it doesn't work. –  Apr 25 '19 at 15:50
  • @marmot @daleif I have tried using ulem, added an update to my question. – Mike Apr 25 '19 at 15:52
  • @marmot Thanks! That looks good, but yeah there is a small gap between the lines. Is there a way to increase the 'margin' around the text? – Mike Apr 25 '19 at 15:55
  • I added what I know as an answer which I will be happy to remove. –  Apr 25 '19 at 15:59

1 Answers1

1

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

enter image description here

if I add (from this answer)

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

I get

enter image description here

  • 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. –  Apr 25 '19 at 16:19
  • Would this TikZ solution be a feasible option do you think? https://tex.stackexchange.com/questions/88977/putting-a-coloured-background-behind-text-without-adding-whitespace – Mike Apr 26 '19 at 10:56