编辑

编辑

下面的代码不起作用。这是因为我的 TeX 文件中有 savenotes,当我删除 savenotes 时它就可以正常工作。有没有办法让我的代码在不删除 savenotes 的情况下正常工作?

以下是代码:

    %---------------------Preamble---------------%
\documentclass[b5paper,twoside,10pt,openany]{book}
\usepackage[margin=1in]{geometry}       
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage[bitstream-charter]{mathdesign}
\usepackage{amsmath}
\usepackage{sectsty}
\usepackage[english]{babel}
\usepackage{microtype}          
\usepackage{natbib}             
\pagenumbering{gobble}          
\usepackage{fancyhdr}           
\usepackage{ragged2e}
\usepackage{booktabs}
\captionsetup[table]{skip=10pt}
\providecommand\phantomsection{}
\setcounter{secnumdepth}{3}
\usepackage{tabu}
\allsectionsfont{\sffamily}
\usepackage[explicit]{titlesec}
\usepackage{tikz}

%\usetikzlibrary{shapes.misc,arrows}
%defining subsection titles
\newcommand\titlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (a) at (3cm,0) {
        \textbf{\arabic{chapter}.\thesection}
    };
}%
}
\titleformat{\section}{\large\sf}{\titlebar}{0.25cm}{\textcolor{black}{#1}}      %% Change color if needed and remove \sf.
\titlespacing*{\section}{-2cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesection}{\arabic{section}}

%defining subsection titles
\newcommand\subtitlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (b) at (3cm,0) {
        \textbf{\arabic{chapter}.\arabic{section}.\thesubsection}
    };
}%
}

\titleformat{\subsection}{\normalfont\sf}{\subtitlebar}{0.2cm}{\textcolor{black}{#1}}  %% Change color if needed and remove \sf.
\titlespacing*{\subsection}{-1.8cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesubsection}{\arabic{subsection}}

%---------------------Document starts here---------------%
\begin{document}    

%---------------------Table of contents---------------%
\pagestyle{empty}
\begin{onehalfspacing}          % here doublespacing can be used. Adjust the margins of the page!
\setcounter{tocdepth}{1}        % to display a detailed table of contents with each subsection
\tableofcontents
\end{onehalfspacing}
%\pagestyle{empty}
%---------------------Adding space in the whole thesis---------------%
\cleardoublepage
\begin{spacing}{1.3}
%\sectionfont{\noindent\fbox}  % makes a box around the section title.

%---------------------Headers, Footers and Page numbers---------------%
\pagestyle{fancy}
\renewcommand{\chaptermark}[1]{ \markboth{#1}{} }
\lhead{}
\chead{}
\renewcommand{\headrulewidth}{0.4pt} %
\renewcommand{\footrulewidth}{0.4pt} %
\pagenumbering{arabic}\setcounter{page}{9}
\fancyhf{}
\fancyhead[LE]{Chapter \thechapter}
\fancyhead[RO]{\nouppercase\leftmark}
\fancyfoot[LE,RO]{\thepage}

%---------------------Chapters---------------%
\chapter{First Chapter}

\section{Introduction}

\begin{savenotes}
\begin{table}

this is a test table

\end{table}
\end{savenotes}

\section{Conclusion}

%------------------------------------%

\end{spacing}
\end{document} 

谢谢Harish,

我提到的第一个问题是由于某种原因:我的 TeX 文件显示与titlesec包不兼容的错误。

以下是代码:

\documentclass[b5paper,twoside,10pt,openany]{book}
%\setlength{\headheight}{15pt}
\usepackage[margin=1in]{geometry}       % sets up the margins of the paper
\usepackage[english]{babel}
\usepackage{charter}
\usepackage[T1]{fontenc}
\usepackage[bitstream-charter]{mathdesign}
\usepackage{amsmath}
\usepackage{sectsty}
\usepackage{titleps}            
\usepackage[english]{babel}
\usepackage{microtype}          
\usepackage{natbib}             
\usepackage{fancyhdr}           
\usepackage{footnote}           
\usepackage{graphicx}           
\usepackage{setspace}           
\usepackage{ragged2e}
\usepackage{lscape}             
\usepackage{threeparttable}     
\usepackage{booktabs}
\usepackage{caption}            
\captionsetup[table]{skip=10pt}
\usepackage[normalem]{ulem}     
\providecommand\phantomsection{}
\setcounter{secnumdepth}{3}
\usepackage{tabu}
\usepackage[explicit]{titlesec}

\begin{document}

this is a test document

\section{test 1}

\section{test 2}

\end{document}

答案1

您可以修改以下代码:这个答案

\documentclass{book}
\usepackage[explicit]{titlesec}
\usepackage{tikz}
%\usetikzlibrary{shapes.misc,arrows}
%defining subsection titles
\newcommand\titlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (a) at (3cm,0) {
        \textbf{\arabic{chapter}.\thesection}
    };   
}%
}
\titleformat{\section}{\large\sf}{\titlebar}{0.25cm}{\textcolor{black}{#1}}      %% Change color if needed.
\titlespacing*{\section}{-2cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesection}{\arabic{section}}

%defining subsection titles
\newcommand\subtitlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (b) at (3cm,0) {
        \textbf{\arabic{chapter}.\arabic{section}.\thesubsection}
    };
}%
}

\titleformat{\subsection}{\normalfont\sf}{\subtitlebar}{0.2cm}{\textcolor{black}{#1}}  %% Change color if needed.
\titlespacing*{\subsection}{-1.8cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesubsection}{\arabic{subsection}}


\begin{document}
\chapter{Some chapter}
Some text here
\section{Nucleic Acids}
Some text here
\subsection{Nucleotides}
Some text here
\end{document}

在此处输入图片描述

根据您的喜好更改尺寸和颜色。

编辑

您加载了太多包,其中一些几乎加载了两倍(charter)。冲突发生在titleps和之间titlesec。如果我评论\usepackage{titleps},冲突就会消失。

\documentclass[b5paper,twoside,10pt,openany]{book} %
\setlength{\headheight}{15pt} \usepackage[margin=1in]{geometry} % sets up the margins of the paper
\usepackage[english]{babel} 
%\usepackage{charter}  %% why to load two times?
\usepackage[T1]{fontenc} 
\usepackage[bitstream-charter]{mathdesign} 
\usepackage{amsmath} 
\usepackage{sectsty} 
%\usepackage{titleps}
\usepackage[english]{babel} 
\usepackage{microtype}
\usepackage{natbib}
\usepackage{fancyhdr}
\usepackage{footnote}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{ragged2e} 
\usepackage{lscape}
\usepackage{threeparttable}
\usepackage{booktabs} 
\usepackage{caption}
\captionsetup[table]{skip=10pt} 
\usepackage[normalem]{ulem}
\providecommand\phantomsection{} \setcounter{secnumdepth}{3} 
\usepackage{tabu} 
\usepackage[explicit]{titlesec}
\usepackage{tikz}
%\usetikzlibrary{shapes.misc,arrows}
%defining subsection titles
\newcommand\titlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (a) at (3cm,0) {
        \textbf{\arabic{chapter}.\thesection}
    };
}%
}
\titleformat{\section}{\large\sf}{\titlebar}{0.25cm}{\textcolor{black}{#1}}      %% Change color if needed and remove \sf.
\titlespacing*{\section}{-2cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesection}{\arabic{section}}

%defining subsection titles
\newcommand\subtitlebar{%
\tikz[baseline,trim left=0cm,trim right=3cm] {
    \node [
        fill=black!90,
        text = white,
        anchor= base east,
        minimum height=3.5ex] (b) at (3cm,0) {
        \textbf{\arabic{chapter}.\arabic{section}.\thesubsection}
    };
}%
}

\titleformat{\subsection}{\normalfont\sf}{\subtitlebar}{0.2cm}{\textcolor{black}{#1}}  %% Change color if needed and remove \sf.
\titlespacing*{\subsection}{-1.8cm}{3.5ex plus 1ex minus .2ex}{2.3ex plus .2ex}
\renewcommand*{\thesubsection}{\arabic{subsection}}


\begin{document}
\chapter{Some chapter}
this is a test document

\section{test 1}

\section{test 2}

\end{document}

在此处输入图片描述

如果您愿意,也可以删除\sf\titleformat{\section}..

相关内容