Menu

HowTo_ReusePageStyles

How to use a page style with scrlayer-scrpage in multiple documents without having to copy its definition from one document to another

Users who frequently create similar documents know the problem of needing the same page style definitions over and over again. To do this, they often copy the loading of the scrlayer-scrpage package and its configuration from one document to the next. The disadvantage of this method is that in case of a general change, the definitions in all documents would have to be changed as well.

If you want to use the same page style definitions for several projects, it is therefore recommended to create an own TeX file or wrapper package with the desired definitions. This should be stored in a user-specific or a local TEXMF tree. Care should be taken to build the directory structure in the TEXMF tree correctly.

For example, in TeX Live you could start with

kpsewhich -var-value=TEXMFHOME

to get the root directory of the private TEXMF tree, create tex/latex/koma-script-contrib (including all parents) and copy the file mypagestyles.tex into this directory.

Under MiKTeX, on the other hand, the directory structure texmf\tex\latex\koma-script-contrib could be created under own documents, for example, and mypagestyles.tex then copied into the koma-script-contrib directory. Then open the MiKTeX-Console (via the Windows-Start-Menu) and add the directory own documents\texmf as the new TEXMF-root. With MiKTeX, it should also be noted that after adding more files to koma-script-contrib, the Filename Database must be renewed using the MiKTeX-Console.

For more information about the installation of private or local files, please refer to the instructions of the respective TeX distribution.

The file mypagestyles.tex stored in this way can then be easily loaded in any document after loading scrlayer-scrpage using \input{mypagestyles.tex}. Alternatively, you can create a wrapper package that loads scrlayer-scrpage and provides additional options for selecting the specific page style. For example, this could look like this:

\ProvidesPackage{my-very-nice-scrlayer-scrpage-extension.sty}[2021-06-18 v0.1 using scrlayer-scrpage with predefined page style]
\newcommand*{\myverynice@pagestyle}{}
\DeclareOption{default}{%
  \renewcommand*{\myverynice@pagestyle}{\ifoot*{}}%
}
\DeclareOption{draftnote}{%
  \renewcommand*{\myverynice@pagestyle}{%
    \ifoot*{Draft: \@date}%
  }%
}
\DeclareOption{internal}{%
  \renewcommand*{\myverynice@pagestyle}{%
    \RequirePackage{xcolor}%
    \ifoot*{\textcolor{red}{For internal use only!}}%
  }%
}
\DeclareOption*{\expandafter\PassOptionsToPackage\expandafter{\CurrentOption}{scrlayer-scrpage}}
\ProcessOptions*
\RequirePackage{scrlayer-scrpage}
\myverynice@pagestyle
\endinput

So the package my-very-nice-scrlayer-scrpage-extension would provide the options default, draftnote and internal which can be used to control that additional information can be displayed in the footer of the page. All other options are passed to the scrlayer-scrpage package.

Now create the same directory structure as explained above for mypagestyle.tex. The new package is then copied to koma-script-contrib as my-very-nice-scrlayer-scrpage-extension.sty. With MiKTeX don't forget to renew the Filename Database!

From now on the package can be used like any other package. It automatically loads scrlayer-scrpage and if needed also xcolor and sets the page style according to the selected options. Without any option it does not change the settings of scrlayer-scrpage. But if, for example, the automark option should always be executed, then in my-very-nice-scrlayer-scrpage-extension.sty the line

\RequirePackage{scrlayer-scrpage}

can be changed to:

\RequirePackage[automark]{scrlayer-scrpage}

Related

Wiki (English): HowTo_PageHeadFoot

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.