1

Suppose I have the following custom package where I have my various titlesec setups defined:

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mytitles}[2018/06/01 Custom Titles Package]
\RequirePackage{titlesec}

\DeclareOption{titlesone}{
\titleformat{\chapter}%
  [display]{}{\itshape\huge\thechapter}{0pt}{}[]

\titleformat{\section}%
  [hang]{\Large\itshape}{\thesection}{1em}{}[]
}

\DeclareOption{titlestwo}{
\titleformat{\chapter}%
  [display]{}{\bfseries\huge\thechapter}{0pt}{}[]

\titleformat{\section}%
  [hang]{\Large\itshape}{\thesection}{1em}{}[]
}

\DeclareOption{titlesthree}{
\titleformat{\chapter}%
  [display]{}{\itshape\huge\thechapter}{0pt}{}[]

\titleformat{\section}%
  [hang]{\Large\bfseries}{\thesection}{1em}{}[]
}

\DeclareOption{titlesfour}{
\titleformat{\chapter}%
  [display]{}{\bfseries\huge\thechapter}{0pt}{}[]

\titleformat{\section}%
  [hang]{\Large\bfseries}{\thesection}{1em}{}[]
}

\ExecuteOptions{titlesone}
\ProcessOptions\relax
\endinput

And then:

\documentclass[12pt, a4paper]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[american]{babel}
\usepackage{microtype}
\usepackage{libertine}
\usepackage{lipsum}
\usepackage[titlestwo]{mytitles}


\begin{document}
\chapter{Test Chapter}
\section{Test Section}
\subsection{Test Subsection}
\lipsum
\end{document}

The problem: Suppose I have five different chapter definitions, and five different section definitions. Further suppose that I am like to use any combination. With my extremely limited knowledge, the way I do is to \DeclareOption for each combination. (In this case, twenty five.) Would it be possible to declare options for chapter and section separately, and load them with something like: \usepackage[chapteroptionthree, sectionoptionfour]{mytitles}?

  • Check an updated solution using the key value syntax. :) – Ruixi Zhang Jun 30 '18 at 16:08
4

Edited: If you have way too many options, I suggest using the key value syntax. Without the key value syntax, you would write

\usepackage[chapteroptionthree, sectionoptionfour]{mytitles}

But with the key value syntax, you would write

\usepackage[chapteroption=3, sectionoption=4]{mytitles}

Solution with key value syntax

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mytitles}[2018/06/30 Custom Titles Package]

\RequirePackage{kvoptions}% https://ctan.org/pkg/kvoptions

\SetupKeyvalOptions{
  family=mytitles,
  prefix=mytitles@
}

\DeclareStringOption[1]{chapteroption}[1]
\DeclareStringOption[1]{sectionoption}[1]

\ProcessKeyvalOptions*

\RequirePackage{titlesec}

\ifnum\mytitles@chapteroption=1
  \titleformat{\chapter}%
    [display]{}{\itshape\huge\thechapter}{0pt}{}[]
\else
  \ifnum\mytitles@chapteroption=2
    \titleformat{\chapter}%
      [display]{}{\bfseries\huge\thechapter}{0pt}{}[]
  \else
    \ifnum\mytitles@chapteroption=3
      \titleformat{\chapter}%
        [display]{}{\itshape\huge\thechapter}{0pt}{}[]
    \else
      \ifnum\mytitles@chapteroption=4
        \titleformat{\chapter}%
          [display]{}{\bfseries\huge\thechapter}{0pt}{}[]
      \else
        \PackageWarningNoLine{mytitles}{%
          Key value `chapteroption=\mytitles@chapteroption'\MessageBreak
          does not exist!%
        }%
      \fi
    \fi
  \fi
\fi

\ifnum\mytitles@sectionoption=1
  \titleformat{\section}%
    [hang]{\Large\itshape}{\thesection}{1em}{}[]
\else
  \ifnum\mytitles@sectionoption=2
    \titleformat{\section}%
      [hang]{\Large\itshape}{\thesection}{1em}{}[]
  \else
    \ifnum\mytitles@sectionoption=3
      \titleformat{\section}%
        [hang]{\Large\bfseries}{\thesection}{1em}{}[]
    \else
      \ifnum\mytitles@sectionoption=4
        \titleformat{\section}%
          [hang]{\Large\bfseries}{\thesection}{1em}{}[]
      \else
        \PackageWarningNoLine{mytitles}{%
          Key value `sectionoption=\mytitles@sectionoption'\MessageBreak
          does not exist!%
        }%
      \fi
    \fi
  \fi
\fi

\endinput

Solution without key value syntax

Simply write 8 options (your example contains only 4) for chapter and section formatting, and then you have access to all 4*4=16 combinations.

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{mytitles}[2018/06/30 Custom Titles Package]
\RequirePackage{titlesec}

\DeclareOption{chapteroptionone}{
\titleformat{\chapter}%
  [display]{}{\itshape\huge\thechapter}{0pt}{}[]}
\DeclareOption{sectionoptionone}{
\titleformat{\section}%
  [hang]{\Large\itshape}{\thesection}{1em}{}[]}

\DeclareOption{chapteroptiontwo}{
\titleformat{\chapter}%
  [display]{}{\bfseries\huge\thechapter}{0pt}{}[]}
\DeclareOption{sectionoptiontwo}{
\titleformat{\section}%
  [hang]{\Large\itshape}{\thesection}{1em}{}[]}

\DeclareOption{chapteroptionthree}{
\titleformat{\chapter}%
  [display]{}{\itshape\huge\thechapter}{0pt}{}[]}
\DeclareOption{sectionoptionthree}{
\titleformat{\section}%
  [hang]{\Large\bfseries}{\thesection}{1em}{}[]}

\DeclareOption{chapteroptionfour}{
\titleformat{\chapter}%
  [display]{}{\bfseries\huge\thechapter}{0pt}{}[]}
\DeclareOption{sectionoptionfour}{
\titleformat{\section}%
  [hang]{\Large\bfseries}{\thesection}{1em}{}[]}

\ExecuteOptions{chapteroptionone}
\ExecuteOptions{sectionoptionone}
\ProcessOptions\relax
\endinput

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.