編輯

編輯

我想創建一種新的標題類型。標題應該如下所示。在此輸入影像描述

標頭的名稱應為“Myheader”。現在我已經解決了這個問題,如下所示:

\makeatletter
\newcommand{\Myheader}{\subsubsection}
\makeatother
\titlespacing*{\subsubsection}{0cm}{0cm}{2pt}%pbk
\titleformat{\subsubsection}[display]{\Huge\filleft\scshape}{ \normalfont\bf\fontfamily{put}\fontseries{b}\fontsize{95pt}{0pt}\selectfont\thesection}{20pt}{}[\titlerule\vspace{2ex}\filright\vspace{2ex}]

我仍然希望能夠使用\subsubsection標頭,因此該解決方案不可接受。程式碼的最小範例是:

\documentclass[10pt,a4paper,oneside]{article}
\usepackage[danish,english]{babel}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\makeatletter
\newcommand{\Myheader}{\subsubsection}
\makeatother
\titlespacing*{\subsubsection}{0cm}{0cm}{2pt}%pbk
\titleformat{\subsubsection}[display]{\Huge\filleft\scshape}{ \normalfont\bf\fontfamily{put}\fontseries{b}\fontsize{95pt}{0pt}\selectfont\thesection}{20pt}{}[\titlerule\vspace{2ex}\filright\vspace{2ex}]
\begin{document}
\Myheader{The first appendix}
\end{document}

知道如何解決這個問題嗎?

答案1

您可以修補為開始附錄而發出的命令,以便\section在該點之後產生正確的結果,即僅針對附錄。這將確保您的附錄放置在部分層次結構中的正確層級(例如,目錄或書籤),並使您的程式碼具有最大程度的靈活性(例如,如果您改變主意,只需刪除補丁即可)。

例如:

\documentclass[10pt,a4paper,oneside]{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{titlesec,etoolbox}
\usepackage{kantlipsum}
\apptocmd\appendix{%
  \titlespacing*{\section}{0cm}{0cm}{2pt}%pbk
  \titleformat{\section}[display]{\Huge\filleft\scshape}{ \normalfont\bfseries\fontfamily{put}\fontseries{b}\fontsize{95pt}{0pt}\selectfont\thesection}{20pt}{}[\titlerule\vspace{2ex}\filright\vspace{2ex}]
}{\typeout{Appendices title formatting successfully patched. Expect the expected.}}{\typeout{Appendices title formatting could not be patched. Unexpected results paradoxically to be expected.}}
\begin{document}
\section{A section}
\kant[1]
\section{Another section}
\kant[2]
\appendix
\section{The first appendix}
\kant[3]
\end{document}

修補的附錄

編輯

在評論中,您詢問如何修改此設定以在發出\section後的頁面上獲取空標題。\appendix您的評論讓我假設您正在使用fancyhdr.如果不知道您目前在其他地方使用的標頭程式碼,則很難指定執行此操作的最佳方法,但如果您有合理的標準,那麼這應該可行。

\documentclass[10pt,a4paper,oneside]{article}
\usepackage{lmodern}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{titlesec,etoolbox,fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhf[lh]{\leftmark}
\fancyhf[rh]{\thepage}
\fancypagestyle{ancy}{%
  \fancyhf[h]{}%
  \renewcommand\headrulewidth{0pt}%
}
\usepackage{kantlipsum}
\apptocmd\appendix{%
  \titlespacing*{\section}{0cm}{0cm}{2pt}%pbk
  \titleformat{\section}[display]{\Huge\filleft\scshape}{ \normalfont\bfseries\fontfamily{put}\fontseries{b}\fontsize{95pt}{0pt}\selectfont\thesection}{20pt}{}[\titlerule\vspace{2ex}\filright\vspace{2ex}\thispagestyle{ancy}]
}{\typeout{Appendices title formatting successfully patched. Expect the expected.}}{\typeout{Appendices title formatting could not be patched. Unexpected results paradoxically to be expected.}}
\begin{document}
\section{A section}
\kant[1-4]
\section{Another section}
\kant[5]
\appendix
\section{The first appendix}
\kant[6-8]
\end{document}

附錄中的新部分沒有標題

請注意,這是對您的描述的字面解釋,可能是也可能不是您想要的。尤其:

  • \section以下\appendix會清空目前頁面上的標題,即使該頁面也包含\sectionbefore \appendix;
  • 如果後面的頁面\appendix不包含\section.

\appendix如果您根本不需要標題。你可以使用:

\apptocmd\appendix{%
  \pagestyle{ancy}%
  \titlespacing*{\section}{0cm}{0cm}{2pt}%pbk
  \titleformat{\section}[display]{\Huge\filleft\scshape}{ \normalfont\bfseries\fontfamily{put}\fontseries{b}\fontsize{95pt}{0pt}\selectfont\thesection}{20pt}{}[\titlerule\vspace{2ex}\filright\vspace{2ex}\thispagestyle{ancy}]
}{\typeout{Appendices title formatting successfully patched. Expect the expected.}}{\typeout{Appendices title formatting could not be patched. Unexpected results paradoxically to be expected.}}

附錄中根本沒有標題

這裡的其他可能性更複雜,所以我只會在必要時考慮這些。在這種情況下,我將使用您當前的標題代碼,因為fancyhdr此時可能最容易擺脫它。titleps補充titlesec並且在這裡會很好地工作。

編輯 編輯

為了回答您關於\tableofcontents以類似方式修補的詢問,您當然可以這樣做。然而,我承認我對一切到底應該是什麼樣子有點迷失,而且我無法真正弄清楚您發布的程式碼的確切用途。 (但是明確的分頁符號和頁碼不應包含在頁面樣式的定義中。)

我這樣做了:

\pretocmd\tableofcontents{%
  \titlespacing*{\section}{0cm}{0cm}{2pt}%pbk
  \titleformat{\section}[display]{\Huge\filleft\scshape}{ \normalfont\bfseries\fontfamily{put}\fontseries{b}\fontsize{95pt}{0pt}\selectfont\thesection}{20pt}{}[\titlerule\vspace{2ex}\filright\vspace{2ex}\thispagestyle{ancy}]
}{\typeout{Contents title formatting successfully patched. Expect the expected.}}{\typeout{Contents title formatting could not be patched. Unexpected results paradoxically to be expected.}}
\apptocmd\tableofcontents{%
  \titlespacing*{\section}{0pt}{*5}{*2.5}%
  \titleformat{\section}[hang]{\normalfont\Large\bfseries}{\thesection}{1.5em}{}[\thispagestyle{fancy}]
}{\typeout{Contents title formatting successfully patched again. Expect the expected.}}{\typeout{Contents title formatting could not be patched again. Unexpected results paradoxically to be expected.}}

這給了我這個

奇特的內容

但我現在懷疑你真的只是使用了錯誤的類別。如果您要使用bookreport更多,無論如何都會為您提供。例如book,您可以得到\frontmatter, \mainmatter and\backmatter which are starting to look suspiciously like code you are trying to create here. If so, don't reinvent the wheel - use the right tool for the job and choosebook or abook` 類別。

相關內容