格式化文章中的標題/部分/小節

格式化文章中的標題/部分/小節

我正在努力解決一個簡單的問題。我正在使用 KOMA 課程

\documentclass[11pt]{scrartcl}%

我需要格式化我的標題標題,以便:

  • 章節標題應由阿拉伯數字後面跟著一個點來標識,例如 1.、2. 等。粗體文字

在此輸入影像描述

  • 副標題應由阿拉伯數字後面跟著一個點來標識,例如 1.1 、 2.1 文本不粗體

在此輸入影像描述

  • 小標題由阿拉伯數字後面跟著一個點來標識。例如1.1.1文字斜體

在此輸入影像描述

對於所有章節標題,標題中的所有主要單字應大寫,例如“Hello Stackoverflow This Is Great”

謝謝!

答案1

有一種簡單的方法可以將點添加到所有切片層級:

\usepackage[numbers=endperiod]{scrartcl}

但實際上您只希望部分有最終點,因此您需要執行以下操作:

\documentclass[]{scrartcl}

\renewcommand*{\sectionformat}{\thesection.\enskip}

有一個簡單的方法可以更改字體:

\setkomafont{subsection}{\normalfont}
\setkomafont{subsubsection}{\normalfont\itshape}

沒有一種簡單的方法可以自動完成標題大小寫。但在這裡調整答案:

可以透過以下方式將titlecaps套件與類別一起使用。scrartcl如果您可以在沒有自動標題大小寫的情況下生活(我個人會自己做),您可以消除所有這部分程式碼。這是完整的範例。

如果您確實想這樣做,那麼施維內巴克的回答稍微簡單一些,因為它修改的程式碼比我修改的程式碼高一級抽象。

\documentclass[]{scrartcl}
\setkomafont{subsection}{\normalfont}
\setkomafont{subsubsection}{\normalfont\itshape}
\renewcommand*{\sectionformat}{\thesection.\enskip}
% The rest of this preamble code is only needed for automatic title casing
\usepackage{titlecaps}
% this is a space separated list of words that should be lowercase
% since you have to add to this list manually, title casing is not truly automatic
\Addlcwords{a an the that to this is are and with}
\makeatletter
% adapted from https://tex.stackexchange.com/a/33215/
% ---- only needed for automatic title case ----
\let\scr@section\section
\let\scr@subsection\subsection
\let\scr@subsubsection\subsubsection
\def\section{\@ifstar\cased@sections\cased@section}
\def\subsection{\@ifstar\cased@subsections\cased@subsection}
\def\subsubsection{\@ifstar\cased@subsubsections\cased@subsubsection}
\def\cased@sections#1{\scr@section*{\titlecap{#1}}}
\def\cased@subsections#1{\scr@subsection*{\titlecap{#1}}}
\def\cased@subsubsections#1{\scr@subsubsection*{\titlecap{#1}}}
\def\cased@section{\@dblarg{\cased@section@}}
\def\cased@subsection{\@dblarg{\cased@subsection@}}
\def\cased@subsubsection{\@dblarg{\cased@subsubsection@}}
\def\cased@section@[#1]#2{%
  \scr@section[\titlecap{#1}]{\titlecap{#2}}}
\def\cased@subsection@[#1]#2{%
  \scr@subsection[\titlecap{#1}]{\titlecap{#2}}}
\def\cased@subsubsection@[#1]#2{%
  \scr@subsubsection[\titlecap{#1}]{\titlecap{#2}}}
\makeatother
% ---- only needed for automatic titlecase ----

\begin{document}

\section{A section with a word that should use uppercase}
\subsection{This is a subsection with most words uppercase}
\subsubsection{This is a subsubsection}
\end{document}

程式碼的輸出

答案2

適應於艾倫斯的精彩回答您可以重新定義\sectionlinesformat而不是\section,\subsection並使\subsubsection標題自動大寫:

\let\orig@sectionlinesformat\sectionlinesformat
\renewcommand*{\sectionlinesformat}[4]{%
  \orig@sectionlinesformat{#1}{#2}{#3}{\titlecap{#4}}%
}%

若要變更目錄條目的大小寫,您可以重新定義\addtocentrydefault

\let\orig@addtocentrydefault\addtocentrydefault
\renewcommand*{\addtocentrydefault}[3]{%
  \orig@addtocentrydefault{#1}{#2}{\titlecap{#3}}%
}

若要變更運行頭的大小寫,您可以重新定義\MakeMarkcase

\let\MakeMarkcase\titlecap

微量元素:

\documentclass[]{scrartcl}
\setkomafont{subsection}{\normalfont}
\setkomafont{subsubsection}{\normalfont\itshape}
\renewcommand*{\sectionformat}{\thesection.\enskip}
\renewcommand*{\sectionmarkformat}{\sectionformat}% also for running head
% The rest of this preamble code is only needed for automatic title casing
\usepackage{titlecaps}
% this is a space separated list of words that should be lowercase
% since you have to add to this list manually, title casing is not truly automatic
\Addlcwords{a an the that to this is are and with}
\makeatletter
\let\orig@sectionlinesformat\sectionlinesformat
\renewcommand*{\sectionlinesformat}[4]{%
  \orig@sectionlinesformat{#1}{#2}{#3}{\titlecap{#4}}%
}%
\let\orig@addtocentrydefault\addtocentrydefault
\renewcommand*{\addtocentrydefault}[3]{%
  \orig@addtocentrydefault{#1}{#2}{\titlecap{#3}}%
}
% and if you also want the case change for \paragraph and \subparagraph:
\let\orig@sectioncatchphraseformat\sectioncatchphraseformat
\renewcommand*{\sectioncatchphraseformat}[4]{%
  \orig@sectioncatchphraseformat{#1}{#2}{#3}{\titlecap{#4}}%
}
\makeatother
\let\MakeMarkcase\titlecap
% ---- only needed for automatic titlecase ----
\pagestyle{headings}% to show the running head
\begin{document}
\tableofcontents
\section{A section with a word that should use uppercase}
\subsection{This is a subsection with most words uppercase}
\subsubsection{This is a subsubsection}
\newpage
Empty page
\end{document}

但如果你喜歡骯髒的把戲,你也可以這麼做:

\documentclass[]{scrartcl}
\setkomafont{subsection}{\normalfont}
\setkomafont{subsubsection}{\normalfont\itshape}
\renewcommand*{\sectionformat}{\thesection.\enskip}
\renewcommand*{\sectionmarkformat}{\sectionformat}% also for running head
% The rest of this preamble code is only needed for automatic title casing
\usepackage{titlecaps}
% this is a space separated list of words that should be lowercase
% since you have to add to this list manually, title casing is not truly automatic
\Addlcwords{a an the that to this is are and with}
\makeatletter
\let\orig@sectionlinesformat\sectionlinesformat
\renewcommand*{\sectionlinesformat}[4]{%
  \orig@sectionlinesformat{#1}{#2}{#3}{\titlecap{#4}}%
  \expandafter\gdef\expandafter\@currenttocentry\expandafter{%
    \expandafter\titlecap\expandafter{\@currenttocentry}%
  }%
  \expandafter\gdef\expandafter\@currentheadentry\expandafter{%
    \expandafter\titlecap\expandafter{\@currentheadentry}%
  }%
}%
\let\orig@sectioncatchphraseformat\sectioncatchphraseformat
\renewcommand*{\sectioncatchphraseformat}[4]{%
  \orig@sectioncatchphraseformat{#1}{#2}{#3}{\titlecap{#4}}%
  \expandafter\gdef\expandafter\@currentheadentry\expandafter{%
    \expandafter\titlecap\expandafter{\@currentheadentry}%
  }%
}
\makeatother
% ---- only needed for automatic titlecase ----
\pagestyle{headings}% to show the running head
\begin{document}
\tableofcontents
\section{A section with a word that should use uppercase}
\subsection{This is a subsection with most words uppercase}
\subsubsection{This is a subsubsection}
\newpage
Empty page
\end{document}

這兩個建議的結果是:

在此輸入影像描述

流行語代碼是\paragraphand \subparagraph

答案3

最後編輯(我不得不編輯答案,因為太多的讚成票並不是真正值得的)

\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage{nameref}
\usepackage{mfirstuc}
\MFUnocap{$f(x)=2\cdot x$}

\renewcommand{\sectionformat}{}
\renewcommand{\subsectionformat}{}
\renewcommand{\subsubsectionformat}{}

\renewcommand*\sectionformat{\normalfont\bfseries\Large}
\renewcommand*\subsectionformat{\normalfont\mdseries\rmfamily\large}
\renewcommand*\subsubsectionformat{\normalfont\itshape\large}



\let\oldsection\section
\makeatletter
\def\section{%
\@ifstar{\@Starred}{\@nonStarred}%
}
\def\@Starred{%
\setkomafont{section}{\sectionformat}%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWith}%
{\@StarredWithout}%
}      
\def\@StarredWith[#1]#2{%
\oldsection*{\capitalisewords{#2}}%
}
\def\@StarredWithout#1{%% This would be simpler if your table of contents is not a section... 
\ifnum\pdfstrcmp{#1}{\contentsname}=0%
    \renewcommand{\sectionformat}{}%
    \setkomafont{section}{\sectionformat}%
    \oldsection*{\contentsname}%
\else%
    \oldsection*{\capitalisewords{#1}}%
 \fi%
}
\def\@nonStarred{%
\setkomafont{section}{\sectionformat\thesection.\space}%
\@ifnextchar[%
{\@nonStarredWith}%
{\@nonStarredWithout}%
}
\def\@nonStarredWith[#1]#2{%
\oldsection[#1]{\capitalisewords{#2}}%
}
\def\@nonStarredWithout#1{%
\oldsection{\capitalisewords{#1}}%
}
\makeatother


\let\oldsubsection\subsection
\makeatletter
\def\subsection{%
\@ifstar{\@Starredss}{\@nonStarredss}%
}
\def\@Starredss{%
\setkomafont{subsection}{\subsectionformat}%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWithss}%
{\@StarredWithout}%
}      
\def\@StarredWithss[#1]#2{%
\oldsubsection*{\capitalisewords{#2}}%
}
\def\@StarredWithoutss#1{%% This would be simpler if your table of contents is not a subsection... 
\ifnum\pdfstrcmp{#1}{\contentsname}=0%
    \renewcommand{\sectionformat}{}%
    \setkomafont{subsection}{\sectionformat}%
    \oldsubsection*{\contentsname}%
\else%
    \oldsubsection*{\capitalisewords{#1}}%
 \fi%
}
\def\@nonStarredss{%
\setkomafont{subsection}{\subsectionformat\thesubsection\space}%
\@ifnextchar[%
{\@nonStarredWithss}%
{\@nonStarredWithoutss}%
}
\def\@nonStarredWithss[#1]#2{%
\oldsubsection[#1]{\capitalisewords{#2}}%
}
\def\@nonStarredWithoutss#1{%
\oldsubsection{\capitalisewords{#1}}%
}
\makeatother

\let\oldsubsubsection\subsubsection
\makeatletter
\def\subsubsection{%
\@ifstar{\@Starredsss}{\@nonStarredsss}%
}
\def\@Starredsss{%
\setkomafont{subsubsection}{\subsubsectionformat}%
\@ifnextchar[%
{\GenericWarning{}{Warning: A starred section can not have parameters. I am going to ignore them!}\@StarredWithsss}%
{\@StarredWithout}%
}      
\def\@StarredWithsss[#1]#2{%
\oldsubsubsection*{\capitalisewords{#2}}%
}
\def\@StarredWithoutsss#1{%% This would be simpler if your table of contents is not a subsection... 
\ifnum\pdfstrcmp{#1}{\contentsname}=0%
    \renewcommand{\sectionformat}{}%
    \setkomafont{subsection}{\sectionformat}%
    \oldsubsubsection*{\contentsname}%
\else%
    \oldsubsubsection*{\capitalisewords{#1}}%
 \fi%
}
\def\@nonStarredsss{%
\setkomafont{subsubsection}{\subsubsectionformat\thesubsubsection\space}%
\@ifnextchar[%
{\@nonStarredWithsss}%
{\@nonStarredWithoutsss}%
}
\def\@nonStarredWithsss[#1]#2{%
\oldsubsubsection[#1]{\capitalisewords{#2}}%
}
\def\@nonStarredWithoutsss#1{%
\oldsubsubsection{\capitalisewords{#1}}%
}
\makeatother

\begin{document}

\tableofcontents

\section{test section 1 with text of non-capitalized first word}

That was the first test section and here comes the second named ``\nameref{sec:secd}''



\section{the second section with equation $f(x)=2\cdot x$}\label{sec:secd}

\section*{a section with star}


\subsection{the first subsection}
And we can use very simple the nameref like this: ``\nameref{subsub}''
\subsubsection{a subsubsection}\label{subsub}

\end{document}

結果: 在此輸入影像描述

編輯:正如@AlanMunn所提到的,這個文檔類別不建議我的方法,我將更新答案以包含這個文檔類別...如果有人使用其他文檔類別達到這個問題,可能可以使用這個方法作為一個簡單的方法。

這是滿足您所需一切的簡單方法...只是不要忘記排除您不想從大寫函數中大寫的方程式或單詞,就像我使用命令 MFUnocap 所做的那樣...否則您將得到錯誤。

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{nameref}
\usepackage[explicit]{titlesec}
\usepackage{mfirstuc}
\MFUnocap{$f(x)=2\cdot x$}


\titleformat{\section}
  {\normalfont\Large\bfseries}{\thesection.}{1em}{\capitalisewords{#1}}
\titleformat{\subsection}
  {\normalfont\large}{\thesubsection}{1em}{\capitalisewords{#1}}
\titleformat{\subsubsection}
  {\normalfont\large\itshape}{\thesubsubsection}{1em}{\capitalisewords{#1}}


\begin{document}

\section{Test section 1 with text of non-capitalized first word}

That was the first test section and here comes the second named ``\nameref{sec:second}''

\section{the second section with equation $f(x)=2\cdot x$}\label{sec:second}

\subsection{the first subsection}

\subsubsection{a subsubsection}

\end{document}

另請注意,nameref 的可能用法不會顯示標題,因為它確實在標題中。因此,如果您想按照 @MichaelPalmer 的建議使用 nameref,則必須手動修復標題。

結果:

在此輸入影像描述

相關內容