0

I'm using the following code but when I want to add \includegraphics{example-image-duck} to argument of section or subsection I got error in this case @Skillmon suggest to add \protect which gives \section{\protect\includegraphics{example-image-duck}} but even though I got error besides I get another error from that code as soon as I use \tableofcontents.

\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{xcolor}
\usepackage[Lenny]{fncychap}
\usepackage{graphics}
\usepackage{graphicx}
\usepackage{float}
\usepackage[explicit]{titlesec}
\usepackage{soul}

\definecolor{titleblue}{HTML}{4a7aa4}

\title{Sections and Chapters}
\author{Educ}
\date{\today}

\newbox\TitleUnderlineTestBox
\newcommand*\TitleUnderline[1]
  {%
    \bgroup
    \setbox\TitleUnderlineTestBox\hbox{\colorbox{titleblue}\strut}%
    \setul{\dimexpr\dp\TitleUnderlineTestBox-.3ex\relax}{.3ex}%
    \ul{#1}%
    \egroup
  }
\newcommand*\SectionNumberBox[1]
  {%
    \colorbox{titleblue}
      {%
        \makebox[2.5em][c]
          {%
            \color{white}%
            \strut
            \csname the#1\endcsname
          }%
      }%
    \TitleUnderline{\ \ \ }%
  }
\titleformat{\section}
  {\Large\bfseries\sffamily\color{titleblue}}
  {\SectionNumberBox{section}}
  {0pt}
  {\TitleUnderline{#1}}
\titleformat{\subsection}
  {\large\bfseries\sffamily\color{titleblue}}
  {\SectionNumberBox{subsection}}
  {0pt}
  {\TitleUnderline{#1}}

\begin{document}

\maketitle
\tableofcontents
\chapter{Lenny}
\chapter{Lenny}
\section{Use this Tools \includegraphics[width=1cm]{example-image-duck} to obtain random points in Geogebra}
\section{Plya's Problem-Solving Cycle}
\subsection{Understand the problem}
\subsection{Devise a Plan}
\subsection{Carry Out the Plan}
\subsection{Look Back}
\section{Second Section}

\section
  {Really long section name that is really long, so long it takes two rows}

\end{document}
Educ
  • 4,362
  • No need to load graphics if you then load graphicx. – Skillmon May 01 '18 at 11:37
  • \protect\includegraphics works fine for me. But you should delete the old toc. – Ulrike Fischer May 01 '18 at 11:39
  • works fine with \protect\includegraphics[width=1cm]{example-image-duck} – samcarter_is_at_topanswers.xyz May 01 '18 at 11:40
  • @UlrikeFischer funny. I used article in my tests (because the first question was using article), and there I get the error ! Argument of \@mkboth has an extra }.. As soon as I use report everything works fine. – Skillmon May 01 '18 at 11:43
  • @UlrikeFischer but there still is an issue as soon as you use \protect\includegraphics. Compilation works, but the line under the caption ends at the image. – Skillmon May 01 '18 at 11:45
  • 2
    @Skillmon you could get the correct line by adding a \mbox around the graphics (to hide it from soul). But with article it will fail for the \section inside \tableofcontents. I would never use soul here -- much too fragile. – Ulrike Fischer May 01 '18 at 11:54
  • @UlrikeFischer yes, it is fragile. How would you create that style? One could use TikZ I guess. Multi-line underlining isn't TeX's strength it seems. – Skillmon May 01 '18 at 14:14
  • In https://tex.stackexchange.com/a/476188/117050 there is a version of this, that uses ulem instead of soul which should be more robust (at the expense of worse on screen display and breaking automatic hyphenation). Just for anybody interested in it. – Skillmon Feb 22 '19 at 21:53

0 Answers0