假设我有以下自定义包,其中titlesec
定义了各种设置:
\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
进而:
\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}
问题:假设我有五种不同的chapter
定义,和五种不同的section
定义。进一步假设我想使用任何组合。凭借我极其有限的知识,我的做法是\DeclareOption
对每个组合进行。(在本例中为二十五个。)是否可以分别声明chapter
和的选项section
,并使用类似以下内容加载它们:\usepackage[chapteroptionthree, sectionoptionfour]{mytitles}
?
答案1
已编辑:如果你有太多选择,我建议使用键值语法。如果没有键值语法,你会写
\usepackage[chapteroptionthree, sectionoptionfour]{mytitles}
但是使用键值语法,你可以写
\usepackage[chapteroption=3, sectionoption=4]{mytitles}
解决方案和键值语法
\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
解决方案没有键值语法
只需为章节和部分格式写入 8 个选项(您的示例仅包含 4 个),然后您就可以访问所有 4*4=16 个组合。
\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