Titlesec 与 tikz

Titlesec 与 tikz

我仍然对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

  1. \filcenter并不是\centering
  2. 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}

相关内容