我仍然对titlesec
和感到困惑tikz
。例如,在下面的代码中,如何将章节编号和标题放在具有自定义内边距的圆角矩形内?
\documentclass[12pt, oneside]{book}
\usepackage[paperwidth=30pc, paperheight=36pc, margin=5pc]{geometry}
\usepackage{titlesec}
\titleformat{\chapter}%
[hang]% shape
{\centering\bfseries\large}% format applied to label+text
{\thechapter}% label
{10pt}% horizontal separation between label and title body
{}% before the title body
[]% after the title body
\usepackage{tikz}
\begin{document}
\chapter{Test Chapter Title}
\end{document}
答案1
诀窍在于将最后一个强制参数的最后一部分\titleformat
作为参数传递标题。
借用 matexmatics 的代码
\documentclass[12pt, oneside]{book}
\usepackage[paperwidth=30pc, paperheight=36pc, margin=5pc]{geometry}
\usepackage{tikz}
\usepackage{titlesec}
\titleformat{\chapter}
[block]% shape
{\filcenter\bfseries\large}% format applied to label+text
{}% label
{0pt}% horizontal separation between label and title body
{\maketitleframe{\thechapter}}% before the title body
\titleformat{name=\chapter,numberless}
[block]% shape
{\filcenter\bfseries\large}% format applied to label+text
{}% label
{0pt}% horizontal separation between label and title body
{\maketitleframe{\ignorespaces}}% before the title body
\newcommand{\maketitleframe}[2]{%
\begin{tikzpicture}
\node[draw,rounded corners] {#1 #2};
\end{tikzpicture}% before the title body
}
\begin{document}
\tableofcontents
\chapter{Test Chapter Title}
\end{document}
对于无序章节,定义格式也很重要。使用作为第一个参数,和\ignorespaces
之间的空格将被忽略。注意#1
#2
\filcenter
并不是\centering
- 0pt 而不是 10pt
如果你想在数字和标题之间留出更多空间
\documentclass[12pt, oneside]{book}
\usepackage[paperwidth=30pc, paperheight=36pc, margin=5pc,showframe]{geometry}
\usepackage{tikz}
\usepackage{titlesec}
\titleformat{\chapter}
[block]% shape
{\filcenter\bfseries\large}% format applied to label+text
{}% label
{0pt}% horizontal separation between label and title body
{\maketitleframe{\thechapter\hspace{10pt}}}% before the title body
\titleformat{name=\chapter,numberless}
[block]% shape
{\filcenter\bfseries\large}% format applied to label+text
{}% label
{0pt}% horizontal separation between label and title body
{\maketitleframe{}}% before the title body
\newcommand{\maketitleframe}[2]{%
\begin{tikzpicture}
\node[draw,rounded corners] {#1#2};
\end{tikzpicture}% before the title body
}
\begin{document}
\tableofcontents
\chapter{Test Chapter Title}
\end{document}
答案2
一种解决方案是使用explicit
包的选项titlesec
。然后可以使用来获取标题#1
。命令\thechapter
被移动到放置的\node
位置。#1
\documentclass[12pt, oneside]{book}
\usepackage[paperwidth=30pc, paperheight=36pc, margin=5pc]{geometry}
\usepackage[explicit]{titlesec}
\titleformat{\chapter}%
[hang]% shape
{\centering\bfseries\large}% format applied to label+text
{}% label
{10pt}% horizontal separation between label and title body
{\begin{tikzpicture}\node[draw,rounded corners] {\thechapter{} #1};\end{tikzpicture}}% before the title body
[]% after the title body
\usepackage{tikz}
\begin{document}
\chapter{Test Chapter Title}
\end{document}