![如何用背景图片管理章节标题位置?](https://linux22.com/image/294150/%E5%A6%82%E4%BD%95%E7%94%A8%E8%83%8C%E6%99%AF%E5%9B%BE%E7%89%87%E7%AE%A1%E7%90%86%E7%AB%A0%E8%8A%82%E6%A0%87%E9%A2%98%E4%BD%8D%E7%BD%AE%EF%BC%9F.png)
我编写了以下代码来设计章节标题,但有两件事我没有成功。
\documentclass{book}
\title{Titre de la thèse}
\author{moi}
\date{\today}
\usepackage{lipsum}
\usepackage[top = 2cm, left = 2cm, right = 2cm, bottom = 2cm, head = 14pt, headsep = .5cm]{geometry}
\usepackage{tikz}
\def\docColor{blue}
\usepackage{titlesec}
\titleformat{\chapter}
{\color{white}\sffamily\Huge\bfseries}
{}{0pt}
{\begin{tikzpicture}[overlay, remember picture]
\fill[\docColor] (current page.north west) -- (current page.north east)
-- ++ (0, -6cm) -- ++ (-\paperwidth, -2cm)
node[pos=.5, fill=black!60, text=white, minimum width=6cm,
inner sep=3mm]
{\sffamily\textsc\chaptername~\thechapter}
-- cycle;
\end{tikzpicture}
}
\titlespacing*{\chapter}{0pt}{0cm}{4cm}
\begin{document}
\maketitle
\chapter{Un premier chapitre, Un premier chapitre, Un premier chapitre}
\lipsum[1-5]
\chapter{Un second chapitre, Un second chapitre, Un second chapitre}
\lipsum[1-5]
\end{document}
1) 我想删除缩进。如您所见,如果标题跨越两行,则它们不会左对齐。
2) 我想将标题移到右侧,以便将图片放在左侧。我想做类似的事情
\parbox[c]{.2\textwidth}{a picture}
\parbox[c]{.8\textwidth}{Chapter title}
答案1
有一种可能性是:
只需注释掉 后面虚假的空白即可
\end{tikzpicture}
。我使用了
explicit
选项titlesec
,然后使用了两个预定义宽度的并排\node
;一个用于图像,另一个用于#1
(实际标题)。我还定义了一个
numberless
用于带星号的章节(例如目录、图表列表等)的变体。
代码(根据需要调整设置):
\documentclass{book}
\usepackage[top = 2cm, left = 2cm, right = 2cm, bottom = 2cm, head = 14pt, headsep = .5cm]{geometry}
\usepackage[explicit]{titlesec}
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{lipsum}
\def\docColor{blue}
\titleformat{\chapter}
{\color{white}\sffamily\Huge\bfseries}
{}{0pt}
{\begin{tikzpicture}[overlay, remember picture]
\fill[\docColor] (current page.north west) -- (current page.north east)
-- ++ (0, -6cm) -- ++ (-\paperwidth, -2cm)
node[pos=.5, fill=black!60, text=white, minimum width=6cm,
inner sep=3mm]
{\sffamily\textsc\chaptername~\thechapter}
-- cycle;
\node[text width=.2\textwidth,anchor=west]
(titleimage)
{\includegraphics[width=\linewidth]{example-image-a}};
\node[text width=.8\textwidth,anchor=west]
(title) at (titleimage.east)
{#1};
\end{tikzpicture}%
}
\titleformat{name=\chapter,numberless}
{\color{white}\sffamily\Huge\bfseries}
{}{0pt}
{\begin{tikzpicture}[overlay, remember picture]
\fill[\docColor] (current page.north west) -- (current page.north east)
-- ++ (0, -6cm) -- ++ (-\paperwidth, -2cm)
-- cycle;
\node[anchor=west,text width=\linewidth]
(title)
{#1};
\end{tikzpicture}%
}
\titlespacing*{\chapter}{0pt}{0cm}{5cm}
\title{Titre de la thèse}
\author{moi}
\date{\today}
\begin{document}
\tableofcontents
\chapter{Un premier chapitre, Un premier chapitre, Un premier chapitre}
\lipsum[1-5]
\chapter{Un second chapitre, Un second chapitre, Un second chapitre}
\lipsum[1-5]
\end{document}
答案2
使用explicit
titlesec 选项,事情就简单了:
\documentclass[french]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\title{Titre de la thèse}
\author{moi}
\date{\today}
\usepackage{babel}
\usepackage{lipsum}
\usepackage[top = 2cm, left = 2cm, right = 2cm, bottom = 2cm, head = 14pt, headsep = .5cm]{geometry}
\usepackage{tikz}
\def\docColor{blue}
\usepackage[explicit]{titlesec}
\titleformat{\chapter}
{\color{white}\setlength\parindent{0pt}\sffamily\Huge\bfseries}
{}{0pt}
{\begin{tikzpicture}[overlay, remember picture]
\fill[\docColor] (current page.north west) -- (current page.north east)
-- ++ (0, -6cm) -- ++ (-\paperwidth, -2cm)
node[pos=.5, fill=black!60, text=white, minimum width=6cm,
inner sep=3mm]
{\sffamily\textsc\chaptername~\thechapter}
-- cycle;
\end{tikzpicture}
\includegraphics[width =0.2\textwidth]{euclid-1945}%
\hfill \parbox[b]{.75\textwidth}{#1\\\mbox{}}}%
\titlespacing*{\chapter}{0pt}{0cm}{4cm}
\begin{document}
\maketitle
\chapter[Un premier chapitre]{ Un premier chapitre, Un premier chapitre, Un premier chapitre, Un premier chapitre}
\lipsum[1-5]
\chapter{Un second chapitre, Un second chapitre, Un second chapitre}
\lipsum[1-5]
\end{document}