我想在标题后插入/嵌入符号/图像。
您知道这是否可以做到吗?
答案1
一种可能的解决方案是使用titlesec
和tcolorbox
(根据您的需要进行最适合的调整):
\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}
\tcbset{
bgtable/.style={
freelance,
frame code={}
center title,
left=0pt,
right=0pt,
top=0pt,
bottom=0pt,
watermark graphics=bricks.jpeg,
watermark stretch=1.00,
watermark opacity=0.7,
arc=0pt,
outer arc=0pt,
nobeforeafter}
}
\titleformat{\section}
{\normalfont\Large\bfseries}{}{0em}
{%
\setbox0=\hbox{\Large\bfseries\thesection\hspace{1em}}%
\begin{tcolorbox}[bgtable]
\thesection\hspace{1em}\hangindent\wd0\strut#1\strut%
\end{tcolorbox}%
}
\titleformat{name=\section,numberless}
{\normalfont\Large\bfseries}{}{0em}
{%
\begin{tcolorbox}[bgtable]
\strut#1\strut
\end{tcolorbox}%
}
\titlespacing*{\section}
{0pt}{4.5ex plus 1ex minus .2ex}{1ex plus .2ex}
\begin{document}
\section{Test numbered section}
\lipsum[4]
\section*{Test unnumbered section}
\lipsum[4]
\section{Test numbered section with a long title; in fact, it spans two lines}
\lipsum[4]
\end{document}
我的例子只涉及\section
和\section*
,但同样的想法可以很容易地适应其他部分单位。
在评论中,需要在标题左侧添加图像;在这种情况下,不需要包tcolorbox
,可以使用\llap
一些框:
\documentclass{article}
\usepackage[explicit]{titlesec}
\usepackage{graphicx}
\usepackage{lipsum}
\newcommand\SecImage{%
\llap{\makebox[3em][l]{%
\raisebox{-.65\totalheight}[0pt][0pt]{%
\includegraphics[width=2em]{example-grid-100x100pt}%
}%
}%
}%
}
\titleformat{\section}
{\normalfont\Large\bfseries}{}{0em}
{%
\setbox0=\hbox{\Large\bfseries\thesection\hspace{1em}}%
\SecImage\thesection\hspace{1em}\hangindent\wd0\strut#1\strut%
}
\titleformat{name=\section,numberless}
{\normalfont\Large\bfseries}{}{0em}
{\SecImage#1}
\begin{document}
\section{Test numbered section}
\lipsum[4]
\section*{Test unnumbered section}
\lipsum[4]
\section{Test numbered section with a long title; in fact, it spans two lines}
\lipsum[4]
\end{document}