使用标题格式自定义目录中的部分

使用标题格式自定义目录中的部分

我想使用 正确枚举目录中的章节\titleformat。我使用了下面的代码,但章节数量不正确,如附图所示!

如果可以的话,谢谢您的帮助。

\documentclass{book}
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage{tikz}\usetikzlibrary{shapes.misc}



%%%%%%%%%%%%%%%%%%%%%%
\newcommand\titlebar{%
\tikz[baseline,trim left=3.1cm,trim right=3cm] {
    \fill [cyan!25] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=cyan!60!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.5ex] at (3cm,0) {
        \textbf{\arabic{chapter}}
    };
}%
}
\titleformat{\chapter}{\large}{\titlebar}{0.1cm}{}



%%%%%%%%%%%%%%%%%%%%%%


\newcommand\titlebaree{%
\tikz[baseline,trim left=3.1cm,trim right=3cm] {
    \fill [cyan!25] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=green!60!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.5ex] at (3cm,0) {
        \textbf{\arabic{chapter}.\thesection}
    };
}%
}
\titleformat{\section}{\large}{\titlebaree}{0.1cm}{}


%%%%%%%%%%%%%%%%%%%%%%

\renewcommand*{\thesection}{\arabic{section}}

\newcommand\titlebare{%
\tikz[baseline,trim left=3.1cm,trim right=3cm] {
    \fill [cyan!25] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=red!60!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.5ex] at (3cm,0) {
        \textbf{\arabic{subsection}.\thesubsection}
    };
}%
}
\titleformat{\subsection}{\large}{\titlebare}{0.1cm}{}



\begin{document}

\tableofcontents

\chapter{First Chapter}
\section{Section One}
\lipsum[1]
\subsection{Subsection Two}
\section{Section name}
\lipsum[1]
\subsection{Subsection One}
\lipsum[1]



\end{document}

在此处输入图片描述

答案1

不要重新定义和在和中分别\thesection使用和:\thesection\thesubsection\titlebaree\titlebare

\documentclass{book}
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage{tikz}\usetikzlibrary{shapes.misc}

%%%%%%%%%%%%%%%%%%%%%%
\newcommand\titlebar{%
\tikz[baseline,trim left=3.1cm,trim right=3cm] {
    \fill [cyan!25] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=cyan!60!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.5ex] at (3cm,0) {
        \textbf{\arabic{chapter}}
    };
}%
}
\titleformat{\chapter}{\large}{\titlebar}{0.1cm}{}

%%%%%%%%%%%%%%%%%%%%%%


\newcommand\titlebaree{%
\tikz[baseline,trim left=3.1cm,trim right=3cm] {
    \fill [cyan!25] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=green!60!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.5ex] at (3cm,0) {
        \textbf{\thesection}% <- changed
    };
}%
}
\titleformat{\section}{\large}{\titlebaree}{0.1cm}{}


%%%%%%%%%%%%%%%%%%%%%%

%\renewcommand*{\thesection}{\arabic{section}}% <- removed

\newcommand\titlebare{%
\tikz[baseline,trim left=3.1cm,trim right=3cm] {
    \fill [cyan!25] (2.5cm,-1ex) rectangle (\textwidth+3.1cm,2.5ex);
    \node [
        fill=red!60!white,
        anchor= base east,
        rounded rectangle,
        minimum height=3.5ex] at (3cm,0) {
        \textbf{\thesubsection}% <- changed
    };
}%
}
\titleformat{\subsection}{\large}{\titlebare}{0.1cm}{}

\begin{document}
\tableofcontents

\chapter{First Chapter}
\section{Section One}
\lipsum[1]
\subsection{Subsection One}
\section{Section name}
\lipsum[1]
\subsection{Subsection One}
\lipsum[1]
\subsection{Subsection Two}
\lipsum[1]
\subsection{Subsection Three}
\lipsum[1]
\end{document}

结果:

在此处输入图片描述

在此处输入图片描述

相关内容