tikz
的 overlay
本来就“不占空间”,它把图片叠在文字上。删掉之后还要把 \titlespacing
设置的间距改回来。
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{tikz}
\usepackage{varwidth}
\usetikzlibrary{shadows,calc}
\usepackage[explicit]{titlesec}
\titleformat{\section}
{}
{}
{-.5em} %左右移动\thesection标签位置
{\mysectionformat{#1}}
\newcommand{\mysectionformat}[1]{%
\makebox[0pt][l]{\def\rad{7pt}\def\Rad{3.5pt}\def\tlength{3cm}\def\theight{0.8cm}%
\begin{tikzpicture}%[remember picture,overlay]
\coordinate (sec) at (.6cm,0);
\fill[color=teal!80!black] ([xshift=\tlength-\Rad,yshift=5pt]sec.north west) coordinate (secnw) to[out=0,in=90,looseness=0.7] ([xshift=\tlength+0.5*\Rad]sec.north west) coordinate (NW)--([xshift=-2*\rad]NW)--cycle;
\path[fill=teal,drop shadow={opacity=0.3,shadow xshift=.05cm,shadow yshift=-.05cm}]
([xshift=\rad,yshift=5pt]sec.north west) coordinate (topleft) to[out=180,in=0,looseness=1] ([xshift=-3*\rad,yshift=-\theight]sec.north west) --([xshift=\tlength-4*\rad,yshift=-\theight]sec.north west) coordinate (bottomright) to[out=0,in=180] ([xshift=\tlength,yshift=5pt]sec.north west) --cycle;
\draw[teal,line width=0.5pt] (NW)--++(.8\linewidth,0);
\coordinate (SecName) at ($(topleft)!0.5!(bottomright)$);
\node[text=white,font=\rmfamily\large\bfseries] at (SecName) {Sec \thesection};
\node[text=black,font=\rmfamily\bfseries\Large,below right] (secnum) at ([shift={(.1cm,-.16cm)}]secnw) {\begin{varwidth}{.8\linewidth}\setlength\baselineskip{22.5pt}#1\end{varwidth}};
\end{tikzpicture}}}
\titlespacing{\section}{0pt}{0pt}{0pt}
\title{test}
\author{1479840692 }
\date{March 2023}
\usepackage{lipsum}
\begin{document}
\maketitle
\section{Introduction}
\lipsum[1]
\section{\lipsum[2]}
\lipsum[3]
\end{document}
至于加上 \makebox[0pt][l]
则是避免盒子 overfull。
问 遇到超长的标题,使用titlesec的explicit功能定制的section样式无法产生正确间距,如何解决?