2

I am struggling with a simple problem. I am using the KOMA class

\documentclass[11pt]{scrartcl}%

I need to format my titles headings so that:

  • Section headings should be identified by Arabic numbers followed by a dot e.g. 1., 2., etc. Text in Bold

enter image description here

  • Subheadings should be identified by Arabic numbers followed by a dot, e.g 1.1 , 2.1 Text Not Bold

enter image description here

  • Subsubheadings be identified by Arabic numbers followed by a dot. e.g 1.1.1 Text Italicized.

enter image description here

For all section titles, all main words in the title should be capitalized, e.g. “Hello Stackoverflow This Is Great”

Thanks!!!!

  • 2
    Your question is tagged 'koma-script'. Are you in fact using koma-script? If so, maybe look at tex.stackexchange.com/questions/224331/…. Otherwise, the titlesec package may help you. Regarding the upper-case, it would probably be best to just type it in exactly as you want it to appear. – Michael Palmer Jul 6 '17 at 21:17
  • 1
    You already tagged with titlesec... why don't you just search for it's usage? the number of section is \thesection and you can put it in \arabic... \arabic{\thesection}.\arabic{\thesubsection}.... etc... If you just search 10 minutes you will do it yourself – koleygr Jul 6 '17 at 21:19
  • 1
    for the title case, see tex.stackexchange.com/questions/170817/… – user36296 Jul 6 '17 at 21:24
  • @michaelpalmer thanks! yes i use the koma scratchl class. the syntax shown on the link you posted is a bit intimidating.. i only need to get these simple headings... could you please help me a bit? – ℕʘʘḆḽḘ Jul 6 '17 at 21:29
  • 2
    Did you manage to do it yourself? If not, just add in your question a MWE and I will compete it to your desired results – koleygr Jul 6 '17 at 22:22
12

There's a simple way to add the dots to all sectioning levels:

\usepackage[numbers=endperiod]{scrartcl}

But in fact you want only sections to have final dots, so you need to do the following:

\documentclass[]{scrartcl}

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

There's a simple way to change the fonts:

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

There's not a simple way to do title case automatically. But adapting the answer here:

it's possible to use the titlecaps package with the scrartcl class in the following way. If you can live without automatic title case (I would personally just do it yourself) you can eliminate all of this part of the code. Here's the full example.

If you do want to do this, then Schweinebacke's answer is slightly simpler since it modifies code one level of abstraction higher than the code I modified.

\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}

output of code

  • Thanks !! I see you have too many dots here ;-) would your solution work with my document class? how to get rid of the extra dots in the sub and subsubsection? Thanks!!! – ℕʘʘḆḽḘ Jul 7 '17 at 1:46
  • PS while nice, automatic uppercase is not needed – ℕʘʘḆḽḘ Jul 7 '17 at 1:53
  • 1
    @Noobie I've updated the solution. And yes, this does work with your document class, assuming you are indeed using the KOMA class (scrartcl). I would do away with the title case stuff myself. – Alan Munn Jul 7 '17 at 1:59
  • upvoted! did not realized about the need of so much coding at first! – koleygr Jul 7 '17 at 2:08
6

In adaption of Alans's great answer you could redefine \sectionlinesformat instead of \section, \subsection and \subsubsection to make the automatic uppercase of the headings:

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

To change the case of the entries to the table of contents, you could redefine \addtocentrydefault:

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

And to change the case of the running head, you could redefine \MakeMarkcase:

\let\MakeMarkcase\titlecap

MWE:

\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}

But if you like dirty tricks, you could also do:

\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}

Both suggestions result in:

enter image description here

The catch phrase code is for \paragraph and \subparagraph.

6

Last edit (I had to edit the answer because of too many upvotes it did not really deserved)

\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}

Result: enter image description here

Edit: As @AlanMunn mentioned my method is not suggested with this documentclass and I am going to update the answer to include this documentclass... If someone reached this question using other documentclass probably can use this method as an easy one.

Here is an easy way for everything you need... Just don't forget to exclude the equations or the words you don't want to capitalize out of the capitalize function as I did with the command MFUnocap... otherwise you will have errors.

\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}

Also see that the possible usage of nameref will not show the title as it really is in the title. So you have to manually fix the titles if you want to use nameref as @MichaelPalmer suggested.

Result:

enter image description here

  • amazing! would that work with my document class? – ℕʘʘḆḽḘ Jul 7 '17 at 1:45
  • 2
    Hi @Noobie... I thought you was creating the MWE... Yes... it works... Just in case you have already changed the default arabic numbers to something else... remember to change the \thesection \thesubsection etc in \arabic{\thesection}, \arabic{\thesubsection} etc in the commands I gave you... just checked it with your documentclass and works fine – koleygr Jul 7 '17 at 1:59
  • 2
    @koleygr It's not really recommended to use titlesec with the KOMA classes. Your document displays a big warning to that effect if compiled using scrartcl. – Alan Munn Jul 7 '17 at 2:00
  • 1
    thanks @AlanMunn... just show it in my output... I will update... thanks. – koleygr Jul 7 '17 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.