9

Is there a way to define a new section that is similar to \section but with a different font size? Something like

\documentclass{article}
\newcommand{mysection}{\large \section}
\begin{document}

\mysection{Section 1}

\end{document}

I don't want to change the font size of \section permanently.

Alan Munn
  • 218,180
lala_12
  • 356
  • Should it be numbered? Is it outside or inside a \section? The easiest solution is to use the sectioning commands available and format them as you like using titlesec. – Alan Munn Oct 01 '17 at 17:24
  • @AlanMunn yes it should be numbered and it's outside a \section. It should act exactly like \section. – lala_12 Oct 01 '17 at 17:28

1 Answers1

14

There really are a number of different ways to achieve what you want. Some are simpler than others and may work just as well.

Use existing levels if possible

It really only makes sense to add a higher level to your document if you also need all of the lower levels of your document. So for example, if your document requires 4 levels (one above \section and two below), then you can simply use \section, \subsection, \subsubsection and \paragraph as your four levels, and use the titlesec package to format them as you like. This is really the simplest method. If you don't like the names you can always use \let to change them to something that matches your document semantics better.

Use report class and redefine the \chapter command

Alternatively, you could use a class that has a division above section, such as the \report class and redefine the format of the \chapter command to be your outside sectioning command. I've made the fontsize \LARGE as an example but you can change this as you require. The values for the spacing are taken from the titlesec documentation for the default class sectioning definitions.

If you don't like using \chapter for something that isn't a chapter, you can use \let to rename it to something that matches your document semantics, e.g., \let\mysection\chapter.

\documentclass{report}
\usepackage{titlesec}
\titleformat{\chapter}[hang]
  {\normalfont\LARGE\sffamily\bfseries}{\thechapter}{1em}{}
\titlespacing*{\chapter}{0pt}{3.5ex plus 1ex minus .2ex}{2.3 ex plus .2ex}
\usepackage{lipsum}
\begin{document}
\chapter{A chapter}
\lipsum[1]
\section{A section}
\lipsum[2]
\end{document}

Redefining the \part command in the article class

If you don't want to change the document class, you could also redefine the \part command which is above the \section level in article to be like a section:

\documentclass{article}
\usepackage{titlesec}
\titleclass{\part}{straight}
\titleformat{\part}[hang]
  {\normalfont\LARGE\bfseries}{\thepart}{1em}{}
\titlespacing*{\part}{0pt}{3.5ex plus 1ex minus .2ex}{2.3 ex plus .2ex}
\renewcommand{\thepart}{\arabic{part}}
\let\mysection\part
\usepackage{lipsum}
\begin{document}
\mysection{A chapter}
\lipsum[1]
\section{A section}
\lipsum[2]
\end{document}

Creating a new titling level entirely

If you still need parts in your document, and all the levels below section you can create a new sectioning class entirely.

\documentclass{article}
\usepackage{titlesec}
\usepackage{chngcntr}
\newcounter{mysection}
\titleclass{\mysection}{straight}[\part]
\titleformat{\mysection}[hang]
  {\normalfont\LARGE\bfseries}{\themysection}{1em}{}
\titlespacing*{\mysection}{0pt}{3.5ex plus 1ex minus .2ex}{2.3 ex plus .2ex}
\renewcommand{\themysection}{\arabic{mysection}}
\counterwithin{section}{mysection}
\usepackage{lipsum}
\begin{document}
\mysection{A chapter}
\lipsum[1]
\section{A section}
\lipsum[2]
\end{document}

Tables of contents

If you take the last option, you need to also tell the TOC about your new section if you are using a table of contents. To do this, use the titletoc package which works nicely with titlesec. In this complete example I've added a new sectioning level between \part and \section and then defined its counter (with the help of the chngcntr package). I then use the \titlecontents command from titletoc to tell the TOC how to format the \mysection level.

\documentclass{article}
\usepackage{titlesec}
\usepackage{titletoc}
\usepackage{chngcntr}
\newcounter{mysection}
\titleclass{\mysection}{straight}[\part]
\titleformat{\mysection}[hang]
  {\normalfont\LARGE\bfseries}{\themysection}{1em}{}
\titlespacing*{\mysection}{0pt}{3.5ex plus 1ex minus .2ex}{2.3 ex plus .2ex}
\renewcommand{\themysection}{\arabic{mysection}}
\renewcommand{\thesection}{\themysection.\arabic{section}}
\counterwithin{section}{mysection}
\contentsmargin{1em}
\titlecontents*{mysection}[2em]{}{\bfseries\contentslabel{2em}}{}
{\titlerule*[.5pc]{.}\contentspage}[]
\dottedcontents{section}[4em]{}{2em}{.5pc}
\usepackage{lipsum}
\usepackage{hyperref}
\begin{document}
\tableofcontents
\mysection{My custom section}
\lipsum[1]
\section{A section}
\lipsum[2]
\end{document}

output of code

Alan Munn
  • 218,180
  • Thank you! However, I would really like to keep the \article class, since it changes a lot of my formatting otherwise. – lala_12 Oct 01 '17 at 19:50
  • @lala_12 I've added two other solutions using article class. – Alan Munn Oct 01 '17 at 20:12
  • Thank you! I get the error Package hyperref Warning: bookmark level for unknown mysection defaults to 0, but it still works. – lala_12 Oct 01 '17 at 20:27
  • I didn't see this before, but the table of contents cannot be displayed correctly.. Do you know how to change to bookmark level so it is looks like a subsection in the table of contents? – lala_12 Oct 01 '17 at 21:15
  • @lala_12 I've updated the answer. (You might also want to consider the first option I've added too.) – Alan Munn Oct 03 '17 at 05:46
  • @AlanMunn: I have tried using your code, but inserting two mysection on a row, so ... \mysection{A chapter} One \mysection{another} Two ... This gives an error: "Package titlesec Error: Entered in horizontal mode. \mysection{another}". What is going on? – Filippo Alberto Edoardo Nov 17 '19 at 08:41
  • @FilippoAlbertoEdoardo The code as posted works without error. The problem probably lies with your titlesec definitions, but without seeing your code it's not possible to diagnose. Make a minimal document that shows the problem and ask a new question, linking to this question as the source of the original code. – Alan Munn Nov 17 '19 at 18:30
  • @AlanMunn Done in the question https://tex.stackexchange.com/q/516856/30052. Thanks! – Filippo Alberto Edoardo Nov 17 '19 at 22:19
  • @FilippoAlbertoEdoardo Thanks! I've added an answer and updated this one too. – Alan Munn Nov 19 '19 at 17:04