2

I have been trying to use a tcolorbox within a titleformat call to use part of an image in each section title. Unfortunately, it seems as though it gets stuck with passing the section title through to the tcolornox, such that calling {#1} within the tcolorbox throws up an error

\titleformat{\section} [block] {\center\normalfont\bfseries\color{Red}} {} {0pt} { \begin{tcolorbox}[enhanced, width=\textwidth, watermark graphics=graphic.jpg, watermark opacity=0.2, watermark overzoom=1.0, colback=Red!10, colframe=Red!80, sharp corners, valign=center, height=1.5cm] \begin{center} \textbf{#1} \end{center} \end{tcolorbox} } 

The error message that comes up at each section call:

Illegal parameter number in definition of \ttlf@section

Any help would be greatly appreciated

EDIT: To note, the code works when #1 is replaced with 'test', however the section title is printed underneath the tcolorbox rather than within it.

1 Answer 1

1

You could load titlesec with the explicit option, but I usually don't recommend this. The last mandatory argument of \titleformat can end with a one-argument macro that will be fed with the section title.

\documentclass{article} \usepackage[many]{tcolorbox} \usepackage{titlesec} \colorlet{Red}{red!60} \newcommand{\formatsectiontitle}[1]{% \begin{tcolorbox}[ enhanced, width=\textwidth, watermark graphics=example-image, % <--- fix the file name watermark opacity=0.2, watermark overzoom=1.0, colback=Red!10, colframe=Red!80, sharp corners, valign=center, height=1.5cm ] \centering\bfseries #1 \end{tcolorbox}% } \titleformat{\section}[block] {\filcenter\normalfont\bfseries\color{Red}} {} {0pt} {\formatsectiontitle} \begin{document} \section{Test title} Here the section starts. \end{document} 

enter image description here

3
  • 1
    Fantastic. Thanks so much Commented Feb 21, 2021 at 11:04
  • @MargotRosa You still have to manage the section numbers; or are you omitting them throughout? Commented Feb 21, 2021 at 11:30
  • Yeah, omitting them was intentional Commented Feb 23, 2021 at 0:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.