2

I have defined my sectioning as follows:

\documentclass{article}
\usepackage{titlesec,xcolor,wrapfig,lipsum}
\definecolor{titelkleur}{HTML}{92D851}
\titleformat{\section}
 {\fontsize{28}{32}\sffamily\bfseries\raggedright}%\underline
 {}
 {-0.5cm}
 {\textcolor{titelkleur}{Knoop\thesection:} 
\hspace{3pt}\textcolor{titelkleur}}
                   [\color{titelkleur}\titlerule]%\vspace{-3ex}\color{titelkleur}\rule{.5\columnwidth}{2pt}]
\begin{document}
\section{test}
\begin{wrapfigure}{l}{.3\textwidth}
\rule{3.5cm}{12cm}
\caption{emptyfigure}
\end{wrapfigure}
\lipsum[1]
\section{section with line crossing wrapfigure}
\end{document}

but when a section title is next to a wrapfigure, the titlerule overlaps with the figure. Is there any way to stop this from happening?

0

2 Answers 2

3

One can put \section inside a minipage, but that causes two other problems. First, the minipage will be treated as a single line of text, confusing wrapfigure into thinking it is only \baselineskip in height. (That is still a problem any time you change font size or add extra space.) This was fixed by adding lots of fake lines to compensate.

The second problem is that the effects of \@afterheading are lost. The easy fix is to add \noindent to the next line of text.

\documentclass{article}
\usepackage{titlesec,xcolor,wrapfig,lipsum}
\definecolor{titelkleur}{HTML}{92D851}
\titleformat{\section}
 {\fontsize{28}{32}\sffamily\bfseries\raggedright}%\underline
 {}
 {-0.5cm}
 {\textcolor{titelkleur}{Knoop\thesection:} 
\hspace{3pt}\textcolor{titelkleur}}
                   [\color{titelkleur}\titlerule]%\vspace{-3ex}\color{titelkleur}\rule{.5\columnwidth}{2pt}]

\newcommand{\wrapspacer}[1]% #1 = special text
{\ifhmode\par\fi\leavevmode\bgroup
  \sbox0{\begin{minipage}{\linewidth}\hrule height0pt
    #1\hrule height0pt
    \end{minipage}}%
  \dimen0=\dimexpr \ht0+\dp0\relax
  \loop\ifdim\dimen0>\baselineskip
    \strut\vspace{-\baselineskip}\newline
    \advance\dimen0 by -\baselineskip
  \repeat
  \noindent\usebox0
  \hrule height0pt
\egroup}

\begin{document}
\section{test}
\begin{wrapfigure}{l}{.3\textwidth}
\rule{3.5cm}{12cm}
\caption{emptyfigure}
\end{wrapfigure}
\lipsum[1]
\wrapspacer{\section{section with line crossing wrapfigure}}
\noindent\lipsum[2]
\end{document}
1
  • One of the best suggestion
    – MadyYuvi
    Jul 30, 2023 at 16:24
2

This answer replaces \titlerule in the definition of the section header formatting with \rule{\linewidth}. Now the length of the rule is proportional to the linewidth.

To get the text to respect the placement of the wrapfig item, it is placed in an adjustbox. The adjustbox is defined with spacing before and after the box, vspace=\parskip and set in a minipage with a width equal to \linewidth. The end of the wrapfig is defined with a strut{}, otherwise the text wrapping ends too late (i.e. the space allowed for the image is too large.

I replaced lipsum with blindtext passages to set smaller chunks of text, which better illustrates the text wrapping around the figure.

This is the result:

enter image description here

This is the MWE:

\documentclass[a4paper]{article}
\usepackage[margin=2.5cm]{geometry}
\usepackage[english]{babel}
\usepackage[pangram]{blindtext}
\usepackage{titlesec,xcolor,wrapfig,adjustbox}
\definecolor{titelkleur}{HTML}{92D851}
\titleformat{\section}
 {\fontsize{28}{32}\sffamily\bfseries\raggedright}%\underline
 {}
 {-0.5cm}
 {\textcolor{titelkleur}{Knoop\thesection:} 
\hspace{3pt}\textcolor{titelkleur}}
%                   [\color{titelkleur}\titlerule]%\vspace{-3ex}\color{titelkleur}\rule{.5\columnwidth}{2pt}] 
                    [\color{titelkleur}\rule{\linewidth}{0.4mm}]    

\begin{document}
\section{test}
\begin{adjustbox}{
        vspace=\parskip,
        minipage=[c]{1.0\linewidth}
    }
\begin{wrapfigure}{l}{.3\linewidth}
\centering \rule{3.5cm}{8cm} 
\caption{emptyfigure}
\end{wrapfigure}
\strut{}        % This defines the end of the wrapfigure. It is required to get the correct linewidth for the rule under the heading
\Blindtext[1][10]
\section{section with line crossing wrapfigure}
\Blindtext[1][10]
\end{adjustbox} % place \end{adjustbox} at an appropriate place to end wrapping
\Blindtext[1][10]
\end{document}
1
  • 1
    Note: \rule is in hmode and takes a full \baselineskip. \hrule is in vmode and only takes 2\lineskip plus the rule height. Sep 9, 2017 at 14:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .