我仍然對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}