创建在样式之间来回切换的文档类的优雅方法

创建在样式之间来回切换的文档类的优雅方法

我想创建一个图书类,class.cls比如

  1. 它会根据代码在主文档中的位置自动获取某种样式。例如,每个代码都具有一种样式:\frontmatter\mainmatter\backmatter

  2. 事实上,我甚至可能想要改变其中的风格\frontmatter,所以我希望有某种命令可以在已经定义的风格之间交替class.cls

为了具体起见,我们以\pagestyle命令或为例\titleformat。我想做类似以下的事情。

  \documentclass[14pt, a4paper]{extbook} 
  \usepackage{fancyhdr}
  \usepackage[explicit]{titlesec}
  \usepackage{psvectorian}

  \begin{document}

  \frontmatter
  \pagestyle{empty}

  \input{file1.tex}

  \pagestyle{fancy}
  \fancyhf{}
  \fancyhead[LE,RO]{\leftmark}
  \fancyhead[RE,LO]{\rightmark}
  \fancyfoot[CE,CO]{\thepage}

  \titleformat{\chapter}[block]
               {\huge\normalfont\bfseries}
               {}{0pt}{\centering \psvectorian[scale=0.5]{60}}
               \titlespacing{\chapter}{0pt}{-0.5cm}{20pt}

  \input{file.2}

  \mainmatter

  \pagestyle{empty}

  \titleformat{\chapter}[block]
               {\huge\normalfont\bfseries}
               \titlespacing{\chapter}{0pt}{-0.5cm}{20pt}

  \input{file.3}

  \end{document}

\frontmatter因此,当转到 时,它会再次更改其中的章节、标题和页脚样式\mainmatter

当然,我可以创建一个.cls包含初始设置的文件,然后在主文档中更改这些设置。不过,我希望有一个更优雅的解决方案。

提前致谢。

答案1

我认为,这是一个可以实现您所有要求的 MWE:

\documentclass[14pt, a4paper]{extbook} 
\usepackage[T1]{fontenc}  % << added for a better result
\usepackage{lmodern}      % << but optionnal
\usepackage{fancyhdr}
\usepackage{titlesec}     % << option explicit removed
\usepackage{psvectorian}

\usepackage{etoolbox}     % needed for \appto and \ifstrequal

% packages & settings  for demonstration purpose, 
%not needed  for real use
\usepackage{xcolor}
\usepackage{showframe}
\setlength{\fboxrule}{0.2pt}\setlength{\fboxsep}{-\fboxrule}
\usepackage{lipsum}
\renewcommand*\ShowFrameColor{\color{red}}
\renewcommand*\ShowFrameLinethickness{.1pt}
% end of packages & settings  for demonstration purpose

\makeatletter  % enable the use of @ character

% defining the regular style as in book or in extbook
\newcommand\setStyleRegular[1][]{%
   % style change mid of page doesn't really make sense  
   % to override, pass "samepage"
   \ifstrequal{#1){samepage}{\relax}%
   {if@openright\cleardoublepage \else \clearpage\fi}
   \pagestyle{headings}
   \titleformat{\chapter}[display]{\normalfont\huge\bfseries}%
      {\chaptertitlename\ \thechapter}{20pt}{\Huge}
   \titleformat{\section}{\normalfont\Large\bfseries}{\thesection}{1em}{}
    \if@mainmatter 
        \renewcommand\thepage{\roman{page}}
    \else 
        \renewcommand\thepage{\arabic{page}}
    \fi
}

% defining a fancy style
\newcommand\setStyleFancy[1][]{%
   \setlength{\headheight}{18pt}
   % style change mid of page doesn't really make sense  
   % to override, pass "samepage"
   \ifstrequal{#1){samepage}{\relax}%
   {if@openright\cleardoublepage \else \clearpage\fi}
   \pagestyle{fancy}
   \fancyhf{}
   \fancyhead[LE,RO]{\leftmark}
   \fancyhead[RE,LO]{\rightmark}
   \fancyfoot[CE,CO]{\thepage}
   % for real use, remove the \fbox but keep the \parboxb
   \titleformat{\chapter}[block]{\huge\normalfont\bfseries}%
      {\thechapter}{20pt}{}%
     [\fbox{\parbox{\textwidth}{\centering\psvectorian[scale=0.5]{60}}}]
    \titlespacing{\chapter}{0pt}{-0.5cm}{20pt}  
    \if@mainmatter 
        \renewcommand\thepage{\roman{page}}
    \else 
        \renewcommand\thepage{\arabic{page}}
    \fi
}

% add here any other custom style(s)
\makeatother

%% automatic switch when changing the *-matter
%% no additionnal page changes are needed here
\appto{\frontmatter}{\setStyleRegular[samepage]}
\appto{\mainmatter}{\setStyleFancy[samepage]}

%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\frontmatter
\chapter{Regular in frontmatter}
\section*{A starred section (front)}
\lipsum[1-2]
    
\setStyleFancy

\chapter{Fancy in frontmatter}
\section*{Another starred section  (front)}
\lipsum[7-11]

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\mainmatter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Fancy in mainmatter}
\section{A  section (main)}
\lipsum[1-2]
    
\setStyleRegular

\chapter{Regular in maintmatter}
\section{Another section  (main)}
\lipsum[7-11]

\end{document}
  • 您在使用\titleformat\psvectorian时发现一些异常,现已更正。
  • 因为我没有你的file1,2,3,所以我用lipsum虚拟文本替换了它们
  • \thechapter在花哨的设置中添加了,但如果你不需要它,你可以删除或自定义它;无论如何它都不会出现在前言章节中
  • 当然,这(必须)是定制的,例如添加更多样式,或调整样式更改,\frontmatter或者\mainmatter,如果需要,将其扩展到\backmatter
  • \makeatletter和之间包含的所有代码都\makeatother可以放在\input-ed .tex 文件中,或者自定义包或类中,但后者的创建是一个完全不同的问题,在网上有大量的记录,或者值得在这里再发一篇文章。

相关内容