% \iffalse meta-comment
% ======================================================================
% scrkernel-typearea.dtx
% Copyright (c) Markus Kohm, 2002-2022
%
% This file is part of the LaTeX2e KOMA-Script bundle.
%
% This work may be distributed and/or modified under the conditions of
% the LaTeX Project Public License, version 1.3c of the license.
% The latest version of this license is in
%   http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX 
% version 2005/12/01 or later and of this work.
%
% This work has the LPPL maintenance status "author-maintained".
%
% The Current Maintainer and author of this work is Markus Kohm.
%
% This work consists of all files listed in MANIFEST.md.
% ======================================================================
%%% From File: $Id: scrkernel-typearea.dtx 3889 2022-06-09 12:11:14Z kohm $
%<identify>%%%            (run: identify)
%<init>%%%            (run: init)
%<options>%%%            (run: options)
%<body>%%%            (run: body)
%<final>%%%            (run: final)
%<package&identify>\NeedsTeXFormat{LaTeX2e}[1995/06/01]
%<*dtx>
\ifx\ProvidesFile\undefined\def\ProvidesFile#1[#2]{}\fi
\begingroup
  \def\filedate$#1: #2-#3-#4 #5${\gdef\filedate{#2/#3/#4}}
  \filedate$Date: 2022-06-09 14:11:14 +0200 (Do, 09. Jun 2022) $
  \def\filerevision$#1: #2 ${\gdef\filerevision{r#2}}
  \filerevision$Revision: 3889 $
  \edef\reserved@a{%
    \noexpand\endgroup
    \noexpand\ProvidesFile{scrkernel-typearea.dtx}[%
                            \filedate\space\filerevision\space
                            KOMA-Script
  }
\reserved@a
%</dtx>
%<*dtx|(package&typearea&identify)>
%<package>\ProvidesPackage{typearea}[%
%!KOMAScriptVersion
  package 
%<*dtx>
  source
%</dtx>
  (type area)]
%</dtx|(package&typearea&identify)>
%<*dtx>
\ifx\documentclass\undefined
  \input scrdocstrip.tex
  \@@input scrkernel-version.dtx
  \@@input scrstrip.inc
  \generate{\usepreamble\defaultpreamble
    \file{typearea.sty}{%
      \from{scrkernel-version.dtx}{package,typearea}%
      \from{scrkernel-typearea.dtx}{package,typearea,identify}%
      \from{scrkernel-basics.dtx}{load}%
      \from{scrkernel-compatibility.dtx}{package,typearea,init}%
      \from{scrkernel-typearea.dtx}{package,typearea,init}%
      \from{scrkernel-compatibility.dtx}{package,typearea,option}%
      \from{scrkernel-typearea.dtx}{package,typearea,options}%
      \from{scrkernel-compatibility.dtx}{package,typearea,body}%
      \from{scrkernel-typearea.dtx}{package,typearea,body}%
      \from{scrkernel-typearea.dtx}{package,typearea,final}%
      \from{scrlogo.dtx}{logo}%
    }%
  }
  \@@input scrstrop.inc
\else
  \let\endbatchfile\relax
\fi
\endbatchfile
\documentclass[USenglish]{koma-script-source-doc}
\usepackage{babel}
\setcounter{StandardModuleDepth}{2}
\begin{document}
\DocInput{scrkernel-typearea.dtx}
\end{document}
%</dtx>
% \fi^^A meta-comment
%
% \changes{v2.95}{2002/06/20}{first version after splitting \file{scrclass.dtx}}
% \changes{v3.24}{2017/07/23}{reorganization of code into semantic portions}
% \changes{v3.24}{2017/07/23}{start of English translation}
% \changes{v3.36}{2022/02/11}{switch over from \cls*{scrdoc} to
%   \cls*{koma-script-source-doc}}
% \changes{v3.36}{2022/02/11}{whole implementation documentation in English}
%
% \GetFileInfo{scrkernel-typearea.dtx}
% \title{Calculation of the Typeing Area with
%   \href{https://komascript.de}{\KOMAScript} Package \pkg*{typearea}
%   and Additional Commands for the \href{https://komascript.de}{\KOMAScript}
%   Classes}
% \author{\href{mailto:komascript@gmx.info}{Markus Kohm}}
% \date{Revision \fileversion{} of \filedate}
% \maketitle
% \begin{abstract}
%   \filename{} contains macros for the typing area. These result not only in
%   the package \pkg*{typearea} but also provide features for the
%   \KOMAScript{} classes.
% \end{abstract}
% \tableofcontents
%
% \section{User Manual}
%
% You can find the user documentation of package \pkg*{typearea} and the
% commands implemented here in the \KOMAScript{} manual, either the German
% \file{scrguide} or the English \file{scrguien}.
%
% \MaybeStop{\PrintIndex}
%
% \section{Implementation of Package \pkg*{typearea} and Additional Class
%   Features}
%
%
% \subsection{Loading packages}
%
% Package \pkg*{typearea} needs package \pkg*{scrkbase}. But this will
% be loaded by \file{scrkernel-basics.dtx} just after the identify run of this
% file. It does not need other packages.
%
% The \KOMAScript{} classes need package \pkg*{typearea}. This will be
% loaded here as an early body action. Nevertheless, at this state the typing
% area option code for the classes has already been loaded. So before loading
% the almost identical option code of the package, some macros have to be
% reset:
%    \begin{macrocode}
%<*class&body>
\let\@BCOR                =\relax
\let\@@BCOR               =\relax
\let\@DIV                 =\relax
\let\@@DIV                =\relax
\let\@headlines           =\relax
\let\@@headlines          =\relax
\let\x@paper              =\relax
\let\x@@paper             =\relax
\let\scr@pti@nerr@r       =\relax
%    \end{macrocode}
% Now we can load \pkg*{typearea}:
% \changes{v2.95}{2002/06/22}{Use \cs{KOMAScriptVersion} while loading
%   \pkg*{typearea}}
% We use \cs{KOMAScriptVersion} to verify the identical versions of
% \KOMAScript{} and the package. See \file{scrkernel-version.dtx} for more
% information about this verification.
%    \begin{macrocode}
\RequirePackage{typearea}[\KOMAScriptVersion]
%</class&body>
%    \end{macrocode}
%
% \subsection{Selection of paper size}
%
% Before we can define page margins and a typing area, we have to define the
% paper size. Beside \pkg*{typearea} also \pkg*{scrletter} uses some of
% this code, because it needs the paper size, too.
%
% \begin{command}{\PaperNameToSize}
% \changes{v3.23}{2017/02/22}{added}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} umbenannt in \cs{Ifstr}}
% This command sets \cs{paperheight} and \cs{paperwidth} depending on
% the name of a paper size. It currently supports the ISO formats of the
% series A, B, C and D and the American formats letter, legal and
% executive. The command is defined using \cs{providecommand} so a class
% can define it different before loading \pkg*{typearea}. The first
% optional argument is the error class. Error classes will be defined using
% \cs{ProvideUnknownPaperSizeError} later. The second argument is the name
% of the paper size.
%    \begin{macrocode}
%<*package&options>
\providecommand*{\PaperNameToSize}[2][warning]{%
  \Ifstr{#2}{letter}{%
    \setlength\paperheight{11in}%
    \setlength\paperwidth {8.5in}%
  }{%
    \Ifstr{#2}{legal}{%
      \setlength\paperheight{14in}%
      \setlength\paperwidth {8.5in}%
    }{%
      \Ifstr{#2}{executive}{%
        \setlength\paperheight{10.5in}%
        \setlength\paperwidth {7.25in}%
      }{%
        \Ifstr{#2}{}{%
          \ISO@PaperNameToSize[#1]{}{}\@nil
        }{%
          \edef\reserved@a{\noexpand\ISO@PaperNameToSize[#1]#2 \noexpand\@nil}%
          \reserved@a
        }%
      }%
    }%
  }%
}
%    \end{macrocode}
% \begin{macro}{\ISO@PaperNameToSize}
% \changes{v3.23}{2017/02/22}{new (internal)}
% \changes{v3.28}{2019/11/18}{\cs{ifnumber} umbenannt in \cs{Ifnumber}}
% For ISO paper sizes a sub-macro is used. It has to split the series and the
% root.
%    \begin{macrocode}
\scr@ifundefinedorrelax{ISO@PaperNameToSize}{%
  \def\ISO@PaperNameToSize[#1]#2#3\@nil{%
    \lowercase{\edef\reserved@a{#2}}%
    \@tempswafalse
    \if \reserved@a a%
      \Ifnumber{#3}{%
        \setlength{\paperwidth}{841sp}%
        \setlength{\paperheight}{1189sp}%
        \@tempswatrue
      }{}%
    \else
      \if \reserved@a b%
        \Ifnumber{#3}{%
          \setlength{\paperwidth}{1000sp}%
          \setlength{\paperheight}{1414sp}%
          \@tempswatrue
        }{}%
      \else
        \if \reserved@a c%
          \Ifnumber{#3}{%
            \setlength{\paperwidth}{917sp}%
            \setlength{\paperheight}{1297sp}%
            \@tempswatrue
          }{}%
        \else
          \if \reserved@a d%
            \Ifnumber{#3}{%
              \setlength{\paperwidth}{771sp}%
              \setlength{\paperheight}{1090sp}%
              \@tempswatrue
            }{}%
          \fi
        \fi
      \fi
    \fi
    \if@tempswa
      \@tempcnta=#3\relax
      \@whilenum\@tempcnta>\z@
      \do{\setlength{\@tempdima}{\paperwidth}%
        \setlength{\paperwidth}{.5\paperheight}%
        \setlength{\paperheight}{\@tempdima}%
        \advance\@tempcnta by\m@ne
      }%
      \multiply\paperwidth by186468
      \multiply\paperheight by186468
    \else
%    \end{macrocode}
% The paper size is not a supported ISO format. If
% \cs{scr@UnknownPaperSize@}\meta{errorclass} is defined, use it to show
% the error. If not, use \cs{scr@UnknownPaperSize@error}. This means,
% \meta{errorclass} \texttt{error} has to be defined.
%    \begin{macrocode}
      \scr@ifundefinedorrelax{scr@UnknownPaperSize@#1}{%
        \scr@UnknownPaperSize@error{#2#3}%
      }{%
        \@nameuse{scr@UnknownPaperSize@#1}{#2#3}%
      }%
    \fi
  }%
}{}
%    \end{macrocode}
% \begin{command}{\ProvideUnknownPaperSizeError}
% \changes{v3.23}{2017/02/22}{added}
% Define a new error class for unknown paper formats. The argument is the new
% error class. Each error class is represented by a command
% \cs{scr@UnknownPaperSize@}\meta{errorclass}. That command also has
% exactly one argument, the unknown paper size.
%    \begin{macrocode}
\providecommand*{\ProvideUnknownPaperSizeError}[1]{%
  \expandafter\providecommand\expandafter*%
  \csname scr@UnknownPaperSize@#1\endcsname[1]%
}
%    \end{macrocode}
% \begin{macro}{\scr@UnkownPaperSize@error,\scr@UnkownPaperSize@warning,
%               \scr@UnkownPaperSize@info,\scr@UnkownPaperSize@ignore}
% \changes{v3.23}{2017/02/22}{new (internal)}
% Error handling (error classes) for \cs{PaperNameToSize}.
%    \begin{macrocode}
\ProvideUnknownPaperSizeError{error}{%
  \PackageError{%
%<typearea>    typearea%
%<letter>    scrletter%
  }{Unknown paper size #1}{%
    \string\paperwidth\space and \string\paperheight\space stay unchanged,
    because of unknown paper size.%
  }%
}
%<*typearea>
\ProvideUnknownPaperSizeError{warning}{%
  \PackageWarning{%
    typearea%
  }{%
    \string\paperwidth\space and \string\paperheight\space stay
    unchanged, because\MessageBreak
    of unknown paper size #1%
  }%
}
\ProvideUnknownPaperSizeError{info}{%
  \PackageInfo{%
    typearea
  }{%
    \string\paperwidth\space and \string\paperheight\space stay
    unchanged, because\MessageBreak
    of unknown paper size #1%
  }%
}
\ProvideUnknownPaperSizeError{ignore}{}
%</typearea>
%</package&options>
%    \end{macrocode}
% \end{macro}^^A \scr@UnknownPaperSize@error … \scr@UnknownPaperSize@ignore
% \end{command}^^A \ProvideUnknownPaperSizeError
% \end{macro}^^A \ISO@PaperNameToSize
% \end{command}^^A \PaperNameToSize
%
% \begin{option}{a4paper,a5paper,b5paper,letterpaper,legalpaper,executivepaper,
%                landscape,portrait}
% \changes{v2.97d}{2007/10/03}{\cs{PackageInfo} replaced by \cs{PackageInfoNoLine}}
% \changes{v3.01a}{2008/11/21}{the option is standard but not deprecated}
% The paper sizes of the standard classes are a special case. For
% compatibility they are declared explicitly. Note: There is still a pattern
% matching implementation for other paper size options, but the usage of those
% is hardly deprecated.
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@DeclareStandardOption[typearea]{a4paper}{paper=a4}
\KOMA@DeclareStandardOption[typearea]{a5paper}{paper=a5}
\KOMA@DeclareStandardOption[typearea]{b5paper}{paper=b5}
\KOMA@DeclareStandardOption[typearea]{letterpaper}{paper=letter}
\KOMA@DeclareStandardOption[typearea]{legalpaper}{paper=legal}
\KOMA@DeclareStandardOption[typearea]{executivepaper}{paper=executive}
\KOMA@DeclareStandardOption[typearea]{landscape}{paper=landscape}
\KOMA@DeclareStandardOption[typearea]{portrait}{paper=portrait}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}^^A a4paper … portrait
%
% \begin{option}{paper}
% \changes{v2.95}{2002/12/03}{added}
% \changes{v3.01b}{2008/12/09}{height and width swapped}
% \changes{v3.02c}{2009/02/12}{new \opt{paper\quotechar=seascape}}
% \changes{v3.12}{2013/03/05}{status signaling using \cs{FamilyKeyState}}
% \changes{v3.22}{2016/07/29}{paper orientation depends on format name}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% Normally, the paper size should be set using this option. It provides the
% ISO series A, B, C, D, value pairs
% \opt{(\meta{width},\meta{height})} and the orientations
% \opt{portrait}, \opt{landscape} and \opt{seascape}.
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@key{paper}{%
  \Ifstr{#1}{seascape}{%
    \@seascapetrue
    \expandafter\ifnum\scr@ta@v@is@ge{3.22}\relax
      \@landscapetrue
      \scr@setpaperorientation
    \else
      \if@landscape\else\scr@switchpapersizes\@landscapetrue\fi
    \fi
    \FamilyKeyStateProcessed
    \KOMA@kav@remove{.typearea.\scr@pkgextension}{paper}{landscape}%
    \KOMA@kav@remove{.typearea.\scr@pkgextension}{paper}{portrait}%
    \KOMA@kav@remove{.typearea.\scr@pkgextension}{paper}{seascape}%
    \KOMA@kav@add{.typearea.\scr@pkgextension}{paper}{seascape}%
  }{%      
    \Ifstr{#1}{landscape}{%
      \@seascapefalse
      \expandafter\ifnum\scr@ta@v@is@ge{3.22}\relax
        \@landscapetrue
        \scr@setpaperorientation
      \else
        \if@landscape\else\scr@switchpapersizes\@landscapetrue\fi
      \fi
      \FamilyKeyStateProcessed
      \KOMA@kav@remove{.typearea.\scr@pkgextension}{paper}{landscape}%
      \KOMA@kav@remove{.typearea.\scr@pkgextension}{paper}{portrait}%
      \KOMA@kav@remove{.typearea.\scr@pkgextension}{paper}{seascape}%
      \KOMA@kav@add{.typearea.\scr@pkgextension}{paper}{landscape}%
    }{%
      \Ifstr{#1}{portrait}{%
        \expandafter\ifnum\scr@ta@v@is@ge{3.22}\relax
          \@landscapefalse
          \scr@setpaperorientation
        \else
          \if@landscape\scr@switchpapersizes\@landscapefalse\fi
        \fi
        \FamilyKeyStateProcessed
        \KOMA@kav@remove{.typearea.\scr@pkgextension}{paper}{landscape}%
        \KOMA@kav@remove{.typearea.\scr@pkgextension}{paper}{portrait}%
        \KOMA@kav@remove{.typearea.\scr@pkgextension}{paper}{seascape}%
        \KOMA@kav@add{.typearea.\scr@pkgextension}{paper}{portrait}%
      }{%
        \Ifstr{#1}{letter}{%
          \if@landscape
            \setlength\paperheight{8.5in}%
            \setlength\paperwidth {11in}%
          \else
            \setlength\paperheight{11in}%
            \setlength\paperwidth {8.5in}%
          \fi
          \FamilyKeyStateProcessed
          \KOMA@kav@removekey{.typearea.\scr@pkgextension}{paper}%
          \KOMA@kav@add{.typearea.\scr@pkgextension}{paper}{letter}%
          \KOMA@kav@xadd{.typearea.\scr@pkgextension}{paper}{%
            \if@seascape seascape\else
            \if@landscape landscape\else portrait\fi\fi
          }%
        }{%
          \Ifstr{#1}{legal}{%
            \if@landscape
              \setlength\paperheight{8.5in}%
              \setlength\paperwidth {14in}%
            \else
              \setlength\paperheight{14in}%
              \setlength\paperwidth {8.5in}%
            \fi
            \FamilyKeyStateProcessed
            \KOMA@kav@removekey{.typearea.\scr@pkgextension}{paper}%
            \KOMA@kav@add{.typearea.\scr@pkgextension}{paper}{legal}%
            \KOMA@kav@xadd{.typearea.\scr@pkgextension}{paper}{%
              \if@seascape seascape\else
              \if@landscape landscape\else portrait\fi\fi
            }%
          }{%
            \Ifstr{#1}{executive}{%
              \if@landscape
                \setlength\paperheight{7.25in}%
                \setlength\paperwidth {10.5in}%
              \else
                \setlength\paperheight{10.5in}%
                \setlength\paperwidth {7.25in}%
              \fi
              \FamilyKeyStateProcessed
              \KOMA@kav@removekey{.typearea.\scr@pkgextension}{paper}%
              \KOMA@kav@add{.typearea.\scr@pkgextension}{paper}{executive}%
              \KOMA@kav@xadd{.typearea.\scr@pkgextension}{paper}{%
                \if@seascape seascape\else
                \if@landscape landscape\else portrait\fi\fi
              }%
            }{%
              \iso@paper{#1}%
              \if@tempswa
                \FamilyKeyStateProcessed
                \KOMA@kav@removekey{.typearea.\scr@pkgextension}{paper}%
                \KOMA@kav@xadd{.typearea.\scr@pkgextension}{paper}{#1}%
                \KOMA@kav@xadd{.typearea.\scr@pkgextension}{paper}{%
                  \if@seascape seascape\else
                  \if@landscape landscape\else portrait\fi\fi
                }%
              \else
                \expandafter\maybe@pair#1::\@nil
                \if@tempswa
                  \FamilyKeyStateProcessed
                  \KOMA@kav@removekey{.typearea.\scr@pkgextension}{paper}%
                  \expandafter\ifnum \scr@ta@v@is@lt{3.01b}\relax
                    \PackageWarningNoLine{typearea}{%
                      You may have to exchange height and width!\MessageBreak
                      Until KOMA-Script version 3.01b the semantic\MessageBreak
                      of option `paper' was `paper=height:width'\MessageBreak
                      instead of `paper=width:height'.\MessageBreak
                      I've switched back to that old interpretation\MessageBreak
                      because of the given compatiblity settings}%
                    \setlength\paperwidth{\@tempb}%
                    \setlength\paperheight{\@tempa}%
                  \else
                    \setlength\paperwidth{\@tempa}%
                    \setlength\paperheight{\@tempb}%
                  \fi
                  \expandafter\ifnum \scr@ta@v@is@lt{3.22}\relax
                    \if@landscape
                      \KOMA@kav@xadd{.typearea.\scr@pkgextension}{paper}{\@tempa:\@tempb}%
                    \else
                      \KOMA@kav@xadd{.typearea.\scr@pkgextension}{paper}{\@tempb:\@tempa}%
                    \fi
                  \else
                    \scr@setpaperorientation
                    \KOMA@kav@xadd{.typearea.\scr@pkgextension}{paper}{%
                      \the\paperwidth:\the\paperheight}%
                  \fi
                  \KOMA@kav@xadd{.typearea.\scr@pkgextension}{paper}{%
                    \if@seascape seascape\else
                    \if@landscape landscape\else portrait\fi\fi
                  }%
                \else
                  \KOMA@unknown@keyval{paper}{#1}{%
                    `letter', `legal', `executive', `a'<nr>, `b'<nr>,
                    `c'<nr>, `d'<nr>,\MessageBreak
                    <width>`:'<height>, `landscape' and `portrait'%
                  }%
                \fi
              \fi
            }%
          }%
        }%
      }%
    }%
  }%
}
%    \end{macrocode}
% \begin{macro}{\if@seascape,\@seascapetrue,\@seascapefalse}
% \changes{v3.02c}{2009/02/12}{added}
% This switch stores only whether or not the landscape orientation should be
% up-side-down. Currently this makes sense only using \file{dvips}.
%    \begin{macrocode}
\newif\if@seascape
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\if@landscape,\@landscapetrue,\@landscapefalse}
% This switch stores whether using portrait or landscape orientation.
%    \begin{macrocode}
\newif\if@landscape
%    \end{macrocode}
% \end{macro}
% \begin{macro}{\maybe@pair}
% \changes{v2.95}{2002/12/03}{added}
% \changes{v2.97e}{2007/11/26}{works really}
% Test whether the following parameter is of kind \texttt{\meta{first
% argument}:\meta{second argument}}. If so \cs{@tempa} will become \meta{first
% argument} and \cs{@tempb} will become \meta{second argument} and
% \cs{if@tempswa} will become \cs{iftrue}. Otherwise \cs{if@tempswa} is
% \cs{iffalse} and \cs{@tempa} and \cs{@tempb} are not specified.
%    \begin{macrocode}
\newcommand*{\maybe@pair}{}
\def\maybe@pair#1:#2:#3\@nil{%
  \edef\@tempa{#1}\edef\@tempb{#2}%
  \ifx\@tempa\@empty\@tempswafalse\else
    \ifx\@tempb\@empty\@tempswafalse\else\@tempswatrue\fi
  \fi
}
%</package&typearea&options>
%    \end{macrocode}
% \end{macro}^^A \maybe@pair
% \end{option}^^A paper
%
% \subsection{Special handling of \pkg*{geometry}}
%
% \begin{option}{usegeometry}
% \changes{v3.17}{2015/02/07}{added}
% \pkg*{typearea} optionally can pass several settings to package
% \pkg{geometry}. The user decides whether or not to use this.
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@ifkey{usegeometry}{@ta@usegeometry}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}
% 
%
% \subsection{Setting the output paper size}
%
% The document paper format of option \opt{paper} is one thing. It is used
% only for calculation of the dimensions used by \TeX{}. Originally
% \textsc{dvi} does not know something like a paper size. It just contained
% relative movements from the current position starting with the
% origin. Setting up a page size depends on the output driver or back-end.
%
% \begin{option}{pagesize}
% \changes{v2.7g}{2001/04/17}{added}
% \changes{v2.95}{2002/08/27}{new implementation}
% \changes{v3.02c}{2009/02/14}{new \opt{pagesize\quotechar=false}}
% \changes{v3.05a}{2010/03/18}{new \opt{pagesize\quotechar=dvipdfmx}}
% \changes{v3.12}{2013/03/05}{status signaling using \cs{FamilyKeyState}}
% \changes{v3.17a}{2015/05/03}{logic redesigned}
% \changes{v3.20}{2015/07/10}{\file{luatex} as a synonym of \file{pdftex}}
% This option is the main element for telling pdf\TeX, \file{dvips} or V\TeX{}
% which paper size to use. It is also used by some deprecated options. Here
% are the values and their meanings:
% \begin{labeling}[~--]{\opt{automedia}}
% \item[\opt{auto}] combination of options \opt{pdftex} and
%   \opt{dvips}.
% \item[\opt{automedia}] like \opt{auto} with additional setting of
%   \cs{mediawidth} and \cs{mediaheight} if available.
% \item[\opt{dvips}] adds a \file{dvips} \cs{special} to set the paper
%   size. This should be done only once in the document preamble. Usage inside
%   the document body is not fully supported and will raise a  warning.
% \item[\opt{dvipdfmx}] adds a \file{dvipdfmx} \cs{special} to set the
%   paper size.
% \item[\opt{false}] switch of the automatic driver page size setting,
%   i.e., if there are changes inside the document.
% \item[\opt{pdftex}] set \cs{pdfpagewidth} and \cs{pdfpageheight} if
%   available.
% \end{labeling}
%    \begin{macrocode} 
%<*package&typearea&options>
\KOMA@key{pagesize}[auto]{%
  \KOMA@set@ncmdkey{pagesize}{@pagesizelast}{%
    {off}{-1},{no}{-1},{false}{-1},%
    {auto}{0},{automedia}{1},{dvips}{2},{pdftex}{3},{luatex}{3},{dvipdfmx}{4}%
  }{#1}%
  \ifx\FamilyKeyState\FamilyKeyStateProcessed
    \ifcase\number\@pagesizelast\relax
      \edef\@pagesizelast{auto}%
      \if@atdocument
        \set@pdftexpagesize
        \set@dvipspagesize
      \fi
    \or
      \edef\@pagesizelast{automedia}%
      \if@atdocument
        \set@pdftexpagesize
        \set@vtexpagesize
        \set@dvipspagesize
      \fi
    \or
      \edef\@pagesizelast{dvips}%
      \if@atdocument
        \set@dvipspagesize
      \fi
    \or
      \edef\@pagesizelast{pdftex}%
      \if@atdocument
        \set@pdftexpagesize
      \fi
    \or
      \edef\@pagesizelast{dvipdfmx}%
      \if@atdocument
        \set@dvipdfmxpagesize
      \fi
    \else
      \edef\@pagesizelast{false}%
    \fi
    \KOMA@kav@xreplacevalue{.typearea.\scr@pkgextension}%
                           {pagesize}{\@pagesizelast}%
  \fi
}
%    \end{macrocode}
% \begin{macro}{\@pagesizelast}
% This internal macro stores the current value of option \opt{pagesize} and
% is needed to use the correct signalling if option \opt{paper} is used
% inside the document and the initial setup in |\begin{document}|.
%    \begin{macrocode}
\newcommand\@pagesizelast{auto}
\AtBeginDocument{%
  \KOMAExecuteOptions[.typearea.\scr@pkgextension]{pagesize=\@pagesizelast}%
%    \end{macrocode}
% \end{macro}^^A \@pagesizelast
%
% \begin{macro}{\set@pdftexpagesize}
% \changes{v2.95}{2002/08/27}{added}
% \changes{v3.20}{2015/07/10}{if \cs{pdfpage\dots} does not exist
%   \cs{page\dots} will be tried (needed for lua\TeX{}~0.80.1)}
% \begin{macro}{\set@dvipspagesize}
% \changes{v2.95}{2002/08/27}{added}
% \changes{v3.02c}{2009/02/13}{respecting \opt{seascape}}
% \changes{v3.02c}{2009/02/14}{can be used inside the document}
% \changes{v3.12}{2012/02/06}{not expanded
%   \cs{let}\cs{if@tempswa}\cs{if@dvips} does even work is the later is not
%   yet defined}
% \changes{v3.28}{2019/11/18}{\cs{ifnotundefined} renamed into
%   \cs{Ifnotundefined}}
% \begin{macro}{\setnot@dvipspagesize}
% \changes{v2.95}{2002/08/27}{added}
% \begin{macro}{\set@dvipdfmxpagesize}
% \changes{v3.05a}{2010/03/18}{added}
% \begin{macro}{\set@vtexpagesize}
% \changes{v2.95}{2002/08/27}{added}
% \changes{v3.28}{2019/11/18}{\cs{ifnotundefined} renamed into
%   \cs{Ifnotundefined}}
% \begin{macro}{\scr@updatedvipspagesize}
% \changes{v3.02c}{2009/02/14}{added}
% \changes{v3.12}{2013/05/10}{deactivated, if \cs{XeTeXversion} exists}
% Option \opt{pagesize} needs some internal macros.
% \begin{description}
% \item{Attention:} The definition of these macros may change at usage of
%   themselves or at |\begin{document}|.
% \end{description}
% First of all the definition from |\begin{document}|:
%    \begin{macrocode}
  \expandafter\ifnum\scr@ta@v@is@gt{3.02b}\relax
    \renewcommand*{\set@dvipspagesize}{%
%    \end{macrocode}
% \changes{v3.12}{2012/11/23}{use paper size update only for DVI and PS}
% \changes{v3.12}{2012/02/06}{not expanded
%   \cs{let}\cs{if@tempswa}\cs{if@dvips} does even work if the later is not
%   yet defined}
% Test whether unspecified \textsc{dvi} output or specified \textsc{ps} output
% is used. Only in that cases the \textsc{ps} code for paper size updates will
% be used.
%    \begin{macrocode}
      \begingroup
        \@tempswafalse
        \scr@ifdvioutput{\@tempswatrue}{%
          \Ifnotundefined{if@dvips}{%
            \expandafter\let\csname if@tempswa\expandafter\endcsname
            \csname if@dvips\endcsname
          }{}%
        }%
        \if@tempswa
          \endgroup
          \scr@ifundefinedorrelax{AtNextShipout}{%
            \scr@ifundefinedorrelax{AtBeginShipoutNext}{%
              \begingroup
                \protected@edef\reserved@a{\endgroup
                  \scr@updatedvipspagesize}%
              \reserved@a
            }{%
%    \end{macrocode}
% If \cs{AtBeginShipoutNext} is available (mostly because of loading package
% \pkg{atbegshi}) use it so execute the code at least before other code
% that has been added to \cs{shipout}. Unfortunately this is not reliable. If
% \cs{AtBeginShipout} is used after \cs{set@dvipspagesize} (this means the
% recalculation of the typing area and the margins) but before the current
% page has been shipped out the code of such \cs{AtBeginShipout} will be lost!
%    \begin{macrocode}
              \begingroup
                \protected@edef\reserved@a{\endgroup
                  \protect\AtBeginShipoutNext{%
                    \protect\bgroup
                    \protect\global\protect\setbox\AtBeginShipoutBox
                    \protect\vbox{%
                      \scr@updatedvipspagesize
                      \protect\box\AtBeginShipoutBox
                    }%
                    \protect\egroup
                  }}%
              \reserved@a
            }%
          }{%
%    \end{macrocode}
% If \cs{AtNextShipout} is available (mostly because of loading package
% \pkg{everyshi} use it to execute the code at least before other code
% that has been added to \cs{shipout}.  Unfortunately this is not reliable. If
% \cs{AtNextShipout} or \cs{EveryShipout} is used after \cs{set@dvipspagesize}
% (this means the recalculation of the typing area and the margins) but before
% the current page has been shipped out the code of such \cs{AtNextShipout} or
% \cs{EveryShipout} will be lost!
%    \begin{macrocode}
            \begingroup
              \protected@edef\reserved@a{\endgroup
                \protect\AtNextShipout{%
                  \protect\bgroup
                  \protect\global\protect\setbox\protect\@cclv\protect\vbox{%
                    \scr@updatedvipspagesize
                    \protect\box\protect\@cclv
                  }%
                  \protect\egroup
                }}%
            \reserved@a
          }%
        \else
%    \end{macrocode}
% In this case (neither \textsc{DVI} nor \textsc{PS} output) we only have to
% finish the group that has been used to keep changes of \cs{if@tempswa}
% local.
%    \begin{macrocode}
          \endgroup
        \fi
    }%
  \else
    \let\set@dvipspagesize=\setnot@dvipspagesize
  \fi
  \renewcommand*{\set@dvipdfmxpagesize}{%
    \scr@ifdvioutput{%
      \special{papersize=\the\paperwidth,\the\paperheight}%
    }{}%
  }%
}
%    \end{macrocode}
% And now the definition that is used before |\begin{document}| but executed
% the first time inside |\begin{document}|.
% \changes{v3.21}{2016/05/27}{two missing backslashs added}
%    \begin{macrocode}
\newcommand*{\set@pdftexpagesize}{%
  \scr@ifundefinedorrelax{pdfpagewidth}{%
    \scr@ifundefinedorrelax{pagewidth}{}{\pagewidth=\paperwidth}%
  }{\pdfpagewidth=\paperwidth}%
  \scr@ifundefinedorrelax{pdfpageheight}{%
    \scr@ifundefinedorrelax{pageheight}{}{\pageheight=\paperheight}%
  }{\pdfpageheight=\paperheight}%
}
\newcommand*{\set@dvipspagesize}{%
  \begingroup
    \@tempswafalse
    \scr@ifdvioutput{\@tempswatrue}{%
      \Ifnotundefined{if@dvips}{%
        \expandafter\let\csname if@tempswa\expandafter\endcsname
        \csname if@dvips\endcsname
      }{}%
    }{}%
%    \end{macrocode}
% \changes{v3.22}{2016/08/04}{\pkg{preview} workaround}
% If package \pkg{preview} has been loaded with option \opt{tightpage},
% which can be recognized by a defined macro \cs{pr@nextbb}, and if the
% package is active, we do not add \textsc{dvi} specials, because it would
% disturb Xe\LaTeX.
%    \begin{macrocode}
    \scr@ifundefinedorrelax{pr@nextbb}{}{%
      \expandafter\ifx\csname ifPreview\expandafter\endcsname
                      \csname iftrue\endcsname
        \PackageInfo{typearea}{%
          package `preview' with option `tightpage' detected.\MessageBreak
          initial page size special deactivated%
        }%
        \@tempswafalse
      \fi
    }%
    \if@tempswa
      \endgroup
      \AtBeginDvi{%
        \special{papersize=\the\paperwidth,\the\paperheight}%
        \if@seascape\special{! TeXDict begin /landplus90{true}store end }\fi
      }%
      \expandafter\ifnum\scr@ta@v@is@gt{3.02b}\relax
        \let\set@dvipspagesize=\@empty
      \else
        \let\set@dvipspagesize=\setnot@dvipspagesize
      \fi
    \else
    \endgroup
  \fi
}
\newcommand*{\setnot@dvipspagesize}{%
  \scr@ifdvioutput{%
    \PackageWarning{%
      typearea%
    }{%
      With dvi output option `pagesize=auto',\MessageBreak
      `pagesize=automedia' or `pagesize=dvips'\MessageBreak
      may be used only once at preamble.\MessageBreak
      Option ignored%
    }%
  }{}%
}
\newcommand*{\set@dvipdfmxpagesize}{%
  \scr@ifdvioutput{%
    \AtBeginDvi{%
      \special{papersize=\the\paperwidth,\the\paperheight}%
    }%
  }{}%
}
\newcommand*{\set@vtexpagesize}{%
  \Ifnotundefined{mediawith}{\mediawidth=\paperwidth}{}%
  \Ifnotundefined{mediaheight}{\mediaheight=\paperheight}{}%
}
\newcommand*{\scr@updatedvipspagesize}{%
  \scr@ifundefinedorrelax{XeTeXversion}{%
    \special{ps::\@percentchar\@percentchar 
      PageBoundingBox: 0 0 \the\numexpr\paperwidth/\dimexpr 1bp\relax\relax
      \space \the\numexpr\paperheight/\dimexpr 1bp\relax\relax^^J%
      \@percentchar\@percentchar BeginPageSetup^^J%
      << /PageSize [\the\numexpr\paperwidth/\dimexpr 1bp\relax\relax\space
      \the\numexpr\paperheight/\dimexpr 1bp\relax\relax] 
      >> setpagedevice^^J%<<
      0 0 bop^^J%
      \@percentchar\@percentchar EndPageSetup}%
  }{}%
}
%</package&typearea&options>
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{option}
%
% \begin{option}{dvips}
% \changes{v2.7g}{2001/04/17}{added}
% \changes{v2.95}{2002/08/27}{new implementation}
% \changes{v2.98c}{2008/04/09}{replacement changed}
% \changes{v2.98c}{2008/04/09}{info instead of warning}
% \changes{v3.01a}{2008/11/21}{it's a standard option not deprecated}
% This option is a kind of obsolete. It is now implemented using
% \opt{pagesize}. But because this is a standard option using several other
% packages we only show a standard option information instead of a deprecated
% option warning. Note: Usage of \opt{pagesize=automedia} can still result
% in a warning message!
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@DeclareStandardOption[typearea]{dvips}{%
  pagesize=automedia%
}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}
%
% \begin{option}{pdftex}
% \changes{v2.7g}{2001/04/17}{added}
% \changes{v2.95}{2002/08/27}{new implementation}
% \changes{v2.97d}{2007/10/03}{\cs{PackageInfo} replaced by \cs{PackageInfoNoLine}}
% \changes{v3.01a}{2008/11/21}{it's a standard option not deprecated}
% This option is a kind of obsolete. It is now implemented using
% \opt{pagesize}. But because this is a standard option using several other
% packages we only show a standard option information instead of a deprecated
% option warning.
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@DeclareStandardOption[typearea]{pdftex}{%
  pagesize=pdftex%
}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}
%
% \subsection{Influencing left and right margin proportions}
%
% Options like \opt{twoside} or \opt{twocolumn} influence the
% proportions of the left and right margin and indirectly also the top and the
% bottom margin. They are also very important for the document classes.
%
% \begin{option}{oneside}
% \changes{v2.97e}{2007/11/23}{pass option from class to \pkg*{typearea}}
% \changes{v3.01a}{2008/11/21}{it's a standard option not deprecated}
% \begin{option}{twoside}
% \changes{v2.97d}{2007/10/03}{\cs{PackageInfo} replaced by \cs{PackageInfoNoLine}}
% \changes{v2.97e}{2007/11/23}{pass option from class to \pkg*{typearea}}
% \changes{v3.00}{2008/07/09}{new option \opt{twoside\quotechar=semi}}
% \changes{v3.12}{2013/03/05}{status signaling using \cs{FamilyKeyState}}
% \changes{v3.13a}{2014/04/09}{the class also uses status signaling with
%   \cs{FamilyKeyStateProcessed}}
% \changes{v3.17}{2015/03/17}{automatic usage of \cs{flushbottom} and
%   \cs{raggedbottom}}
% \changes{v3.17}{2015/03/17}{early setting of default}
% \changes{v3.17}{2015/03/18}{use \cs{cleardoubleoddpage} if available}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% \begin{macro}{\if@semitwoside,\@semitwosidetrue,\@semitwosidefalse}
% \changes{v3.00}{2008/07/09}{new boolean switch (intern)}
% \begin{macro}{\if@twoside,\@twosidetrue,\@twosidefalse}
% \changes{v3.00}{2008/07/09}{switch also sets \cs{if@semitwoside}}
% Do we have a single sided or double sided layout?
%    \begin{macrocode}
%<*package&typearea&options>
\newif\if@semitwoside
\g@addto@macro\@twosidetrue\@semitwosidefalse
\g@addto@macro\@twosidefalse\@semitwosidefalse
%</package&typearea&options>
%    \end{macrocode}
% \end{macro}
% \end{macro}
%    \begin{macrocode}
%<*(class|(package&typearea))&options>
\KOMA@key{twoside}[true]{%
%<class>  \if@atdocument\else\PassOptionsToPackage{twoside=#1}{typearea}\fi%
%<*package>
  \if@atdocument\if@twoside
      \scr@ifundefinedorrelax{cleardoubleoddpage}{%
        \cleardoublepage
      }{%
        \cleardoubleoddpage
      }%
  \else\clearpage\fi\fi
%</package>
  \Ifstr{#1}{semi}{%
    \@twosidetrue
%<package>    \@semitwosidetrue
    \FamilyKeyStateProcessed
  }{%
    \def\FamilyElseValue{, `semi'}%
    \KOMA@set@ifkey{twoside}{%
%    \end{macrocode}
% \changes{v3.20}{2015/11/02}{not existing order dependency of
%   \opt{twocolumn} and \opt{twoside} fixed}
% \changes{v3.27}{2019/03/05}{no special treatment for letters}
% \changes{v3.27}{2019/03/05}{missing \cs{sloppy} in twocolumn mode added}
% From v3.17 until 3.19a the classes used a local switch
% \cs{if@tempswa}. As a result of this option \opt{twocolumn} was not
% able to recognize double sided layout. Because of this it was essential to
% handle option \opt{twocolumn} before option \opt{twoside}. This has
% been solved using \cs{if@twoside} in the classes too. Another solution
% could have been to move the implementation and execution of
% \opt{twocolumn} before \opt{twoside}. But I don't know whether or not
% this would have been reliable and the solution here is more simple.
%    \begin{macrocode}
      @twoside%
    }{#1}%
  }%
  \ifx\FamilyKeyState\FamilyKeyStateProcessed
%<*class>
    \expandafter\ifnum\scr@v@is@ge{3.17}%
      \if@twoside\flushbottom
      \else
        \if@twocolumn\sloppy\flushbottom\else\raggedbottom\fi
      \fi
    \fi
%</class>
    \KOMA@kav@xreplacevalue{.%
%<class>      \KOMAClassFileName
%<package>      typearea.\scr@pkgextension
    }{twoside}{#1}%
%<*package>
    \if@twoside
      \if@semitwoside
        \@mparswitchfalse
      \else
        \@mparswitchtrue
      \fi
    \else
      \@mparswitchfalse
    \fi%
    \csname recalctypearea\endcsname
%</package>
  \fi
}
%<*class>
%<book>\KOMAExecuteOptions{twoside=true}
%<!book>\KOMAExecuteOptions{twoside=false}
%</class>
%<*package>
\KOMA@kav@xadd{.typearea.\scr@pkgextension}{twoside}{%
  \if@twoside true\else false\fi
}
%</package>
\KOMA@DeclareStandardOption%
%<package>  [typearea]%
  {oneside}{twoside=false}
%</(class|(package&typearea))&options>
%    \end{macrocode}
% \end{option}^^A twoside
% \end{option}^^A oneside
%
% \begin{option}{onecolumn}
% \changes{v2.95}{2002/12/04}{option moved to package \pkg*{typearea}}
% \changes{v2.97d}{2007/10/03}{\cs{PackageInfo} replaced by \cs{PackageInfoNoLine}}
% \changes{v2.97e}{2007/11/23}{the classes pass the option to \pkg*{typearea}}
% \changes{v3.01a}{2008/11/21}{standard option but not deprecated}
% \begin{option}{twocolumn}
% \changes{v2.95}{2002/12/04}{option moved to package \pkg*{typearea}}
% \changes{v2.97e}{2007/11/23}{the classes pass the option to \pkg*{typearea}}
% \changes{v2.98a}{2007/01/08}{passing the correct option}
% \changes{v3.12}{2013/03/05}{status signaling using \cs{FamilyKeyState}}
% \changes{v3.13a}{2014/04/09}{the class also uses status signaling with
%   \cs{FamilyKeyStateProcessed}}
% \changes{v3.20}{2016/02/16}{\cs{sloppy}, \cs{raggedbottom} and
%   \cs{flushbottom} delayed}
% \changes{v3.27}{2019/03/05}{no extra treatment for letters}
% Do we have a single column or two columns layout?
%    \begin{macrocode}
%<*(class|(package&typearea))&options>
\KOMA@key{twocolumn}[true]{%
%<class>  \if@atdocument\else\PassOptionsToPackage{twocolumn=#1}{typearea}\fi%
%<*package>
  \if@atdocument\clearpage\fi
%</package>
  \KOMA@set@ifkey{twocolumn}{@twocolumn}{#1}%
  \ifx\FamilyKeyState\FamilyKeyStateProcessed
    \KOMA@kav@replacebool{.%
%<class>      \KOMAClassFileName
%<package>      typearea.\scr@pkgextension
    }{twocolumn}{@twocolumn}%
%<package>    \csname recalctypearea\endcsname
%<*class>
    \if@atdocument\expandafter\@firstofone
    \else\@ifpackageloaded{typearea}{\expandafter\@firstofone}
                                    {\expandafter\AtEndOfClass}\fi
    {\expandafter\ifnum\scr@v@is@ge{3.17}%
      \if@twocolumn\twocolumn\sloppy\flushbottom
      \else
        \onecolumn
        \if@twoside\flushbottom\else
          \raggedbottom
        \fi
      \fi
    \fi}%
%</class>
  \fi
}
%<*class>
\AtEndOfClass{%
  \expandafter\ifnum\scr@v@is@lt{3.17}%
%<*!letter>
    \if@twocolumn\twocolumn\sloppy\flushbottom
    \else\onecolumn\if@twoside\flushbottom\else\raggedbottom\fi\fi
%</!letter>
%<letter>    \raggedbottom
  \fi
}
\KOMAExecuteOptions{twocolumn=false}
%</class>
%<*package>
\KOMA@kav@xadd{.typearea.\scr@pkgextension}{twocolumn}{%
  \if@twocolumn true\else false\fi
}
%</package>
\KOMA@DeclareStandardOption%
%<package>  [typearea]%
  {onecolumn}{twocolumn=false}
%</(class|(package&typearea))&options>
%    \end{macrocode}
% \end{option}^^A twocolumn
% \end{option}^^A onecolumn
%
%
% \subsection{Influencing size of head and foot and margins}
%
% We have several options to influence the head hight and the foot height and
% whether or not these are part of the typing area or the margins. We have also
% an option to decide whether or not the margin notes are part of the typing
% area or not.
%
% \begin{option}{headlines}
% \changes{v2.95}{2002/12/06}{new implementation using \pkg{keyval}}
% \changes{v3.12}{2013/03/05}{status signaling using \cs{FamilyKeyState}}
% \begin{option}{footlines}
% \changes{v3.12}{2013/03/26}{added}
% These are two of the recommended options to set the height of the page head
% and footer. Here we set the height by the number of base lines. The number
% is a factor to \cs{baselineskip} and therefore a real number.
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@key{headlines}{%
  \begingroup
    \setlength{\@tempdima}{#1\baselineskip}%
  \endgroup
  \def\ta@hh{#1\baselineskip}%
  \FamilyKeyStateProcessed
  \KOMA@kav@replacevalue{.typearea.\scr@pkgextension}{headlines}{#1}%
  \KOMA@kav@replacevalue{.typearea.\scr@pkgextension}{headheight}{#1\baselineskip}%
}
\KOMA@key{footlines}{%
  \begingroup
    \setlength{\@tempdima}{#1\baselineskip}%
  \endgroup
  \def\ta@fh{#1\baselineskip}%
  \FamilyKeyStateProcessed
  \KOMA@kav@replacevalue{.typearea.\scr@pkgextension}{footlines}{#1}%
  \KOMA@kav@replacevalue{.typearea.\scr@pkgextension}{footheight}{#1\baselineskip}%
}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}
% \end{option}
%
% \begin{option}{headheight}
% \changes{v2.98c}{2008/04/06}{added}
% \changes{v3.12}{2013/03/05}{status signaling using \cs{FamilyKeyState}}
% \begin{option}{footheight}
% \changes{v3.12}{2013/03/26}{added}
% Instead of setting the height of the page head or footer by the number of
% base lines, we can set it also using a length or length value. Note, via
% \cs{documenclass} or \cs{usepackage} only length values are supported
% (due to an expansion problem by the \LaTeX{} kernel with both commands).
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@key{headheight}{%
  \begingroup
    \setlength{\@tempdima}{#1}%
  \endgroup
  \def\ta@hh{#1}%
  \FamilyKeyStateProcessed
  \KOMA@kav@replacevalue{.typearea.\scr@pkgextension}{headheight}{#1}%
  \KOMA@kav@removekey{.typearea.\scr@pkgextension}{headlines}%
}
\KOMA@key{footheight}{%
  \begingroup
    \setlength{\@tempdima}{#1}%
  \endgroup
  \def\ta@fh{#1}%
  \FamilyKeyStateProcessed
  \KOMA@kav@replacevalue{.typearea.\scr@pkgextension}{footheight}{#1}%
  \KOMA@kav@removekey{.typearea.\scr@pkgextension}{footlines}%
}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}
% \end{option}
%
% \begin{option}{headinclude}
% \changes{v2.95}{2002/07/08}{new implementation using \pkg{keyval}}
% This options determines whether or not the head is part of the typing
% area.
% \begin{option}{headexclude}
% \changes{v2.95}{2002/07/08}{new implementation using \pkg{keyval}}
% \changes{v2.97d}{2007/10/03}{\cs{PackageInfo} replaced by \cs{PackageInfoNoLine}}
% \changes{v3.01a}{2008/11/21}{deprecated}
% \begin{macro}{\if@hincl,\@hincltrue,\@hinclfalse}
% \changes{v2.95}{2002/07/08}{new implementation using \pkg{keyval}}
% \changes{v2.97c}{2007/04/19}{direct initialization via \cs{if@hsl},
%   if already defined}
% We use a boolean switch to store the decision.
%    \begin{macrocode}
%<*package&typearea&options>
\newif\if@hincl\@ifundefined{if@hsl}{}{\let\if@hincl\if@hsl}
\KOMA@ifkey{headinclude}{@hincl}
\KOMA@DeclareDeprecatedOption[typearea]{headexclude}{headinclude=false}
%</package&typearea&options>
%    \end{macrocode}
% \end{macro}
% \end{option}
% \end{option}
%
% \begin{option}{headsepline}
% \changes{v3.20}{2015/12/12}{\pkg*{typearea} itself reacts on \opt{headsepline}}
% Until \KOMAScript{} v3.20 \pkg*{typearea} was not able to handle
% sequences of options \opt{headsepline} and \opt{headinclude} to get
% always a correct result. Until v3.18a \opt{headinclude} was prioritized
% against \opt{headsepline} if used as global options but not if used via
% \cs{KOMAoptions}. In v3.18a a bug has been fixed with the result that
% \opt{headsepline} was prioritized against \opt{headinclude} if used
% as global options. But intentionally
% \opt{headinclude=false,}\opt{headsepline} or
% \opt{headinclude=true,}\opt{headsepline=false} always should use the
% setting of option \opt{headsepline} independent of setting this as
% global options or using \cs{KOMAoptions}. Similar with
% \opt{headsepline,}\opt{headinclude=false} or
% \opt{headsepline=false,},\opt{headinclude} the setting of
% \opt{headinclude} should be used. To achieve this \pkg*{typearea}
% itself has to interpret not only \opt{headinclude} but also
% \opt{headsepline} (as an alias of \opt{headinclude}).
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@key{headsepline}[true]{%
  \KOMA@set@ifkey{headinclude}{@hincl}{#1}%
  \ifx\FamilyKeyState\FamilyKeyStateProcessed
    \KOMA@kav@replacebool{.typearea.\scr@pkgextension}{headinclude}{@hincl}%
  \fi
}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}^^A headsepline
%
% \begin{option}{footinclude}
% \changes{v2.95}{2002/07/08}{new implementation using \pkg{keyval}}
% \begin{option}{footexclude}
% \changes{v2.95}{2002/07/08}{new implementation using \pkg{keyval}}
% \changes{v2.97d}{2007/10/03}{\cs{PackageInfo} replaced by \cs{PackageInfoNoLine}}
% \changes{v3.01a}{2008/11/21}{deprecated}
% This options determines whether or not the page footer is part of the typing
% area.
% \begin{macro}{\if@fincl,\@fincltrue,\@finclfalse}
% \changes{v2.95}{2002/07/08}{new implementation using \pkg{keyval}}
% \changes{v2.97c}{2007/04/19}{direct initialization via \cs{if@fsl},
%     if already defined}
% We use a boolean switch to store the decision.
%    \begin{macrocode}
%<*package&typearea&options>
\newif\if@fincl\@ifundefined{if@fsl}{}{\let\if@fincl\if@fsl}
\KOMA@ifkey{footinclude}{@fincl}
\KOMA@DeclareDeprecatedOption[typearea]{footexclude}{footinclude=false}
%</package&typearea&options>
%    \end{macrocode}
% \end{macro}
% \end{option}
% \end{option}
%
% \begin{option}{footsepline}
% \changes{v3.23}{2017/03/03}{has to set \cs{if@fincl} instead of \cs{if@hincl}}
% \changes{v3.20}{2015/12/12}{\pkg*{typearea} itself reacts on \opt{footsepline}}
% Until \KOMAScript{} v3.20 \pkg*{typearea} was not able to handle
% sequences of options \opt{footsepline} and \opt{footinclude} to get
% always a correct result. Until v3.18a \opt{footinclude} was prioritized
% against \opt{footsepline} if used as global options but not if used via
% \cs{KOMAoptions}. In v3.18a a bug has been fixed with the result that
% \opt{footsepline} was prioritized against \opt{footinclude} if used
% as global options. But intentionally
% \opt{footinclude=false,}\opt{footsepline} or
% \opt{footinclude=true,}\opt{footsepline=false} always should use the
% setting of option \opt{footsepline} independent of setting this as
% global options or using \cs{KOMAoptions}. Similar with
% \opt{footsepline,}\opt{footinclude=false} or
% \opt{footsepline=false,},\opt{footinclude} the setting of
% \opt{footinclude} should be used. To achieve this \pkg*{typearea}
% itself has to interpret not only \opt{footinclude} but also
% \opt{footsepline} (as an alias of \opt{footinclude}).
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@key{footsepline}[true]{%
  \KOMA@set@ifkey{footinclude}{@fincl}{#1}%
  \ifx\FamilyKeyState\FamilyKeyStateProcessed
    \KOMA@kav@replacebool{.typearea.\scr@pkgextension}{footinclude}{@fincl}%
  \fi
}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}^^A footsepline
%
% \begin{option}{mpinclude}
% \changes{v2.8q}{2002/03/28}{added}
% \changes{v2.95}{2002/07/08}{new implementation using \pkg{keyval}}
% \begin{option}{mpexclude}
% \changes{v2.8q}{2002/03/28}{added}
% \changes{v2.95}{2002/07/08}{new implementation using \pkg{keyval}}
% \changes{v2.97d}{2007/10/03}{\cs{PackageInfo} replaced by \cs{PackageInfoNoLine}}
% \changes{v3.01a}{2008/11/21}{deprecated}
% Some documents have a lot of large margin notes. Optical these margin notes
% are more part of the typing area than the margin. This options determines
% whether or not margin notes should be part of the typing area.
% \begin{macro}{\if@mincl,\@mincltrue,\@minclfalse}
% \changes{v2.95}{2002/07/08}{new implementation using \pkg{keyval}}
% We use a boolean switch to store the decision. Note, if the margin notes are
% part of the typing area, you should increase the divisor.
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@ifkey{mpinclude}{@mincl}
\KOMA@DeclareDeprecatedOption[typearea]{mpexclude}{mpinclude=false}
%</package&typearea&options>
%    \end{macrocode}
% \end{macro}
% \end{option}
% \end{option}
% 
%
% \subsection{Typing area for the emulation of the standard classes}
%
% For the emulation of the standard classes we need a dedicated size of the
% typing area, if and only if no explicit \opt{DIV} has been given by
% option. The emulation of the standard classes is a class feature. We could
% do it before \pkg*{typearea} and then deactivate initial setting of
% margins and typing area. But it is more easy to use \cs{areaset} after
% loading \pkg*{typearea}. The standard letter class cannot be emulated.
%    \begin{macrocode}
%<*class&body&!letter>
\if@scr@emulatestandardclasses
  \Ifstr{\@lastdiv}{areaset}{}{%
    \ifnum\@lastdiv=\z@
      \setlength{\textwidth}{\dimexpr \paperwidth-2in\relax}
      \ifdim \f@size\p@<10.5\p@
          \setlength{\@tempdimb}{345\p@}
      \else\ifdim \f@size\p@<11.5\p@
          \setlength{\@tempdimb}{360\p@}
        \else
          \setlength{\@tempdimb}{390\p@}
        \fi
      \fi
      \if@twocolumn
        \ifdim\@textwidth>2\@tempdimb\relax
          \setlength{\textwidth}{2\@tempdimb}
        \fi
      \else
        \ifdim\textwidth>\@tempdimb
          \setlength{\textwidth}{\@tempdimb}
        \fi
      \fi
      \@settopoint\textwidth
      \setlength{\@tempdima}{\dimexpr \paperheight-3.5in\relax}
      \divide\@tempdima\baselineskip
      \@tempcnta=\@tempdima
      \setlength{\textheight}{\@tempcnta\baselineskip}
      \addtolength{\textheight}{\topskip}
      \edef\reserved@a{%
        \noexpand\@areasetadvancedtrue
        \noexpand\areaset[current]{\noexpand\textwidth}{\noexpand\textheight}%
        \if@areasetadvanced\else\noexpand\@areasetadvancedfalse\fi
      }\reserved@a
    \fi
  }%  
\fi
%</class&body&!letter>
%    \end{macrocode}
%
% \subsection{Using a predefined divisor depending on the font size}
%
% Some years ago \pkg*{typearea} provides the feature to use a hard coded
% or user defined divisor depending on the document font size. The feature to
% define such predefined divisors is deprecated, because it can be used only
% for integer font sizes in pt. Nevertheless, for compatibility reasons it is
% still implemented.
%
% \begin{macro}{\ta@divlist}
% \changes{v2.0e}{1994/10/28}{added}
% \changes{v2.6}{2000/01/04}{the list provides values from font size 1\,pt}
% \begin{macro}{\ta@divlistelse}
% \changes{v2.6}{2000/01/04}{added}
% We need a default settings for 10\,pt, 11\,pt and 12\,pt. But we define it
% only if they has not been defined before. So classes could define other
% defaults before loading \pkg*{typearea}.
%    \begin{macrocode}
%<*package&typearea&init>
\providecommand\ta@divlist{{}{}{}{}{}{}{}{}{}{8}{10}{12}}
%    \end{macrocode}
% \end{macro}
% If a font size does not have a predefined divisor, the following default
% will be used:
%    \begin{macrocode}
\providecommand\ta@divlistelse{1}
%</package&typearea&init>
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ta@divfor}
% We also need a macro to get one of the values stored at \cs{ta@divlist}
% depending on the font size given by the real number argument. The real
% number will be rounded to the nearest integer.
% \changes{v2.4g}{1996/11/04}{usage of the default value list} 
% \changes{v2.6}{2000/01/04}{usage of the extended list}
% \changes{v2.8q}{2002/03/28}{respect option \opt{mpinclude}}
% \changes{v2.96}{2006/08/11}{font size does not need to be an integer}
% \changes{v3.25}{2018/03/05}{paper size test added}
%    \begin{macrocode}
%<*package&typearea&body>
\newcommand*{\ta@divfor}[1]{%
  \ta@div=\z@
  \expandafter\ifnum\scr@ta@v@is@ge{3.25}\relax
    \ifnum \z@=\ifdim \paperwidth<207mm \z@\else\ifdim \paperwidth>213mm \z@
               \else\ifdim \paperheight<294mm \z@\else
               \ifdim \paperheight>300mm \z@\else \@ne\fi\fi\fi\fi
      \PackageInfoNoLine{typearea}{%
        With paper sizes other than (almost) `A4' predefined\MessageBreak
        DIV values do not exist. Using DIV calculation for good\MessageBreak
        line width (unless using `version=3.24` or prior)%
      }%
      \ta@div=\tw@
    \fi
  \fi
  \if@mincl
    \PackageInfo{typearea}{%
      With option `mpinclude' there are no predefined\MessageBreak
      DIV values. Using DIV calculation for good line\MessageBreak
      width%
    }%
    \ta@div=\tw@
  \fi
  \ifnum \ta@div=\z@
    \xdef\@tempa{\ta@divlist{}}%
    \@tempdima=#1pt
    \addtolength{\@tempdima}{.5\p@}%
    \divide\@tempdima by\p@
    \setlength{\@tempdimb}{\@tempdima}%
    \multiply\@tempdimb by\p@
    \ifdim \@tempdimb=#1pt
      \ta@div=#1\relax
      \@whilenum \ta@div >-9 \do {%
        \ifx\@tempa\empty
        \else \xdef\@tempa{\expandafter\@cdr\@tempa\@nil}\fi
        \advance\ta@div by-1
      }%
    \else
      \let\@tempa\empty
    \fi
    \ifx\@tempa\empty
%    \end{macrocode}
% If there is not default in the list we use the general default.
%    \begin{macrocode}
      \setlength{\@tempdima}{#1\p@}%
      \addtolength{\@tempdima}{10\p@}%
      \PackageWarningNoLine{typearea}{%
        DIV for \the\@tempdima\space and used papersize\MessageBreak
        not defined!\MessageBreak
        Using DIV=\ifnum\number\ta@divlistelse>\m@ne
          \ifcase \ta@divlistelse default%
          \or calc%
          \or calc%
          \or classic%
          \else\ta@divlistelse\fi\else\ta@divlistelse\fi
      }%
      \ta@div=\ta@divlistelse\relax
    \else
%    \end{macrocode}
% Otherwise we use the default of the list.
%    \begin{macrocode}
      \xdef\@tempa{\expandafter\@car\@tempa\@nil}%
      \ifx\@tempa\empty
        \ta@div=\ta@divlistelse\relax
      \else
        \ta@div=\@tempa\relax
      \fi
    \fi
  \fi
}
%</package&typearea&body>
%    \end{macrocode}
% \end{macro}
%
%
% \begin{command}{\SetDIVList}
% \changes{v2.1a}{1994/10/28}{new}
% \changes{v2.6}{2000/01/04}{\cs{ta@divlist} holds values for font sizes from 1\,pt}
% \changes{v3.24}{2017/07/23}{deprecated}
% The command is deprecated. Don't use it any longer.
%    \begin{macrocode}
%<*package&typearea&body>
\newcommand*\SetDIVList[1]{%
  \PackageWarning{typearea}{\string\SetDIVList\space is
    deprecated!\MessageBreak
    This command supports only integer font sizes\MessageBreak
    in pt equal or greater than 10pt.\MessageBreak
    Since version 2.6 the command is implemented\MessageBreak
    for compatibility only. You should not use it\MessageBreak
    any longer}%
  \def\ta@divlist{{}{}{}{}{}{}{}{}{}#1}}
%    \end{macrocode}
% \end{command}^^A \SetDIVList
%
% \begin{command}{\SetXDIVList}
% \changes{v2.6}{2000/01/04}{neu}
% \changes{v3.24}{2017/07/23}{deprecated}
% The command is deprecated. Don't use it any longer.
%    \begin{macrocode}
\newcommand*\SetXDIVList[1]{%
  \PackageWarning{typearea}{\string\SetXDIVList\space is
    deprecated!\MessageBreak
    This command supports only integer font sizes\MessageBreak
    in pt equal or greater than 10pt.\MessageBreak
    Since version 3.0 the command is implemented\MessageBreak
    for compatibility only. You should not use it\MessageBreak
    any longer}%
  \def\ta@divlist{#1}}
%</package&typearea&body>
%    \end{macrocode}
% \end{command}^^A \SetXDIVList
%
%
% \subsection{Calculation of typing area and margins}
%
% The main purpose of the package is the calculation of the size of the
% typeing area and the margins depending on a divisor. As an addition a
% binding correction is included.
%
% The parameters and the calculation is needed before the options can be
% processed.
%
% \begin{ilength}{\ta@bcor}
% \changes{v2.2a}{1995/01/25}{definition of the dimension before option
%    \opt{BCOR}}
% \changes{v2.7g}{2001/04/17}{definition using \cs{newlength} instead of
%    \cs{newdimen}}
% We need a length to store the binding correction. It is initializes to 0\,pt
% implicitly by \cs{newlength}.
%    \begin{macrocode}
%<*package&typearea&options>
\newlength{\ta@bcor}
%    \end{macrocode}
% \end{ilength}
%
% \begin{macro}{\ta@div}
% \changes{v2.2a}{1995/01/25}{definition of the dimension before option
%    \opt{BCOR}}
% We need a counter for the divisor (aka \emph{DIV}). It is initializes to
% 0\,pt implicitly by \cs{newcount}. So it is not a \LaTeX{} counter but a
% \TeX{} counter.
%    \begin{macrocode}
\newcount\ta@div
%</package&typearea&options>
%    \end{macrocode}
% \end{macro}
%
% \begin{ilength}{\ta@hblk,\ta@vblk}
% Width of one horizontal resp. height of one vertical block.
%    \begin{macrocode}
%<*package&typearea&body>
\newlength{\ta@hblk}
\newlength{\ta@vblk}
%</package&typearea&body>
%    \end{macrocode}
% \end{ilength}
% 
% \begin{ilength}{\ta@temp}
% Used several times instead of \cs{@tempdima}, \cs{@tempdimb} or another
% temporary dimension or glue of the LaTeX{} kernel. Nevertheless I should
% think about not using this temporary length any longer.
%    \begin{macrocode}
%<*package&typearea&body>
\newlength{\ta@temp}
%</package&typearea&body>
%    \end{macrocode}
% \end{ilength}
%
% \begin{option}{BCOR}
% \changes{v2.2a}{1995/01/25}{added}
% \changes{v2.95}{2002/12/04}{new implementation using \pkg{keyval}}
% \changes{v3.12}{2013/03/05}{status signaling using \cs{FamilyKeyState}}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% The value is written into the length \cs{ta@bcor}. Several symbolic values
% are supported that have to be converted into a length. Note, while loading
% the package symbolic values are nonsense.
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@key{BCOR}{%
  \Ifstr{#1}{current}{}{%
    \setlength{\ta@bcor}{#1}%
  }%
  \FamilyKeyStateProcessed
  \KOMA@kav@xreplacevalue{.typearea.\scr@pkgextension}{BCOR}{#1}%
}
%    \end{macrocode}
% After loading the package we have to do a little bit more, because we need
% to recalculate the areas.
% \changes{v3.13}{2014/06/30}{\cs{FamilyKeyStateProcessed} added}
% \changes{v3.24}{2017/07/23}{\cs{KOMA@kav@xreplacevalue} added}
%    \begin{macrocode}
\AtEndOfPackage{%
  \KOMA@key{BCOR}{%
    \typearea[{#1}]{last}
    \FamilyKeyStateProcessed
    \KOMA@kav@xreplacevalue{.typearea.\scr@pkgextension}{BCOR}{#1}%
  }%
}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}
%
% \begin{option}{DIV}
% \changes{v2.2a}{1995/01/25}{new option}
% \changes{v2.95}{2002/12/04}{new implementation using \pkg{keyval}}
% \changes{v3.12}{2013/03/05}{status signaling using \cs{FamilyKeyState}}
% The value is written into the counter \cs{ta@div}. Several symbolic values
% are supported and have to be converted into a counter. Note, while loading
% the package symbolic values are nonsense.
% \begin{macro}{\@lastdiv}
% \changes{v3.30}{2020/03/24}{init the internal macro}
% This internal macro stores the last value to option \opt{DIV}. It is
% initialized with \cs{relax}.
%    \begin{macrocode}
%<package&typearea&init>\newcommand*{\@lastdiv}{\z@}\let\@lastdiv\relax
%    \end{macrocode}
% \end{macro}^^A \@lastdiv
%    \begin{macrocode}
%<*package&typearea&options>
\def\movet@reserved@a@to@nnil#1\@nnil{\def\reserved@a{#1}}%
\KOMA@key{DIV}[calc]{%
  \edef\@tempa{#1}%
  \def\@tempb{last}\ifx\@tempa\@tempb\let\@tempa=\@lastdiv
    \ifx\@tempa\relax\def\@tempa{current}\fi
  \fi  
  \def\@tempb{current}\ifx\@tempa\@tempb\else
    \def\@tempb{default}\ifx\@tempa\@tempb\ta@div=\z@ \else
      \def\@tempb{calc}\ifx\@tempa\@tempb\ta@div=\tw@ \else
        \def\@tempb{classic}\ifx\@tempa\@tempb\ta@div=\thr@@ \else
          \expandafter\def\expandafter\@tempa\expandafter{\expandafter0\@tempa}%
          \afterassignment\movet@reserved@a@to@nnil
            \@tempcnta=\@tempa\relax\@nnil
          \def\@tempb{\relax}\ifx\reserved@a\@tempb
            \ta@div=\@tempcnta
            \edef\@tempa{\the\ta@div}%
          \else
            \let\@tempa\relax  
          \fi
        \fi
      \fi
    \fi
  \fi
  \ifx\@tempa\relax
    \FamilyKeyStateUnknownValue
  \else
    \FamilyKeyStateProcessed
    \KOMA@kav@xreplacevalue{.typearea.\scr@pkgextension}{DIV}{\@tempa}%
  \fi
}
%    \end{macrocode}
% After loading the package we have to do a little bit more, because we need
% to recalculate the areas.
% \changes{v3.13}{2014/06/30}{\cs{FamilyKeyStateProcessed} added}
% \changes{v3.24}{2017/07/23}{\cs{KOMA@kav@xreplacevalue} added}
%    \begin{macrocode}
\AtEndOfPackage{%
  \KOMA@key{DIV}{%
    \typearea[current]{#1}%
    \FamilyKeyStateProcessed
    \KOMA@kav@xreplacevalue{.typearea.\scr@pkgextension}{DIV}{#1}%
  }%
}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}
%
% \begin{command}{\typearea}
% This is the real reason for the package: calculation of typing area and
% margins providing an optional binding correction and depending on a
% divisor.
% \begin{macro}{\@typearea}
% \changes{v2.4n}{1997/05/28}{moved main calculation into \cs{@typearea}}
% \changes{v2.95}{2002/08/20}{\cs{selectfont} added}
% The calculation itself is done in \cs{@typearea}. This command does not need
% any parameters because it uses \cs{ta@div} and \cs{ta@bcor}.
%    \begin{macrocode}
%<*package&typearea&body>
\newcommand*\@typearea{%
%    \end{macrocode}
% First of all we have to take care to select the correct font. Because
% otherwise we cannot depend on, e.g., \cs{baselineskip}.
%    \begin{macrocode}
  \selectfont
%    \end{macrocode}
% First calculation is about the horizontal block element size:
%    \begin{macrocode}
  \ta@hblk=\paperwidth
  \advance \ta@hblk by-\ta@bcor
  \divide\ta@hblk by\ta@div
%    \end{macrocode}
% The with of the margin note column depends in the horizontal block size,
% whether or not is should be part of the typing area and whether of not we
% have a double sided document.
% \changes{v2.8q}{2002/03/28}{\cs{marginparwidth} depends on
%      \opt{mpinclude}}
%    \begin{macrocode}
  \if@mincl
    \marginparwidth=\ta@hblk
    \advance\marginparwidth by-\marginparsep
  \else
    \if@twoside
      \if@semitwoside
        \marginparwidth=\ta@hblk
      \else
        \marginparwidth=1.5\ta@hblk
      \fi
    \else
      \marginparwidth=\ta@hblk
    \fi
  \fi
%    \end{macrocode}
% The left and right margin resp. the left margin of odd and even pages
% depends also on the horizontal block width. The relation depends on whether
% or not the document is double sided.
%    \begin{macrocode}
  \oddsidemargin=-1in
  \evensidemargin=-1in
  \if@twoside
    \if@semitwoside
      \advance\oddsidemargin by1.5\ta@hblk
      \advance\oddsidemargin by\ta@bcor
%    \end{macrocode}
% \changes{v3.12}{2013/10/15}{use alternating \opt{BCOR} with
%    \opt{twoside\quotechar=semi}}
% From v3.12 the binding correction is part of the inner margin also for
% \opt{twoside=semi}. If compatibility to earlier release is selected the
% binding correction with \opt{twoside=semi} is always added to the left
% margin.
%    \begin{macrocode}
      \expandafter\ifnum\scr@ta@v@is@lt{3.12}\relax
        \evensidemargin=\oddsidemargin
      \else
        \advance\evensidemargin by1.5\ta@hblk
      \fi
    \else
      \advance\oddsidemargin by\ta@hblk
      \advance\oddsidemargin by\ta@bcor
      \advance\evensidemargin by2\ta@hblk
      \if@mincl
        \advance\evensidemargin by\marginparwidth
        \advance\evensidemargin by\marginparsep
      \fi
    \fi
  \else
    \advance\oddsidemargin by1.5\ta@hblk
    \advance\oddsidemargin by\ta@bcor
    \evensidemargin=\oddsidemargin
  \fi
%    \end{macrocode}
% The width of the typing area is the paper width without the three blocks
% used for the margins and without the binding correction.
%    \begin{macrocode}
  \textwidth=\paperwidth
  \advance\textwidth by-\ta@bcor
  \advance\textwidth by-3\ta@hblk
%    \end{macrocode}
% But, if the margin note column should be part of the typing area, it's width
% has to be subtracted from the width of the typing area. We assume to place
% the margin notes at the outer margin. But if the typing area is to narrow
% for the margin note column, the width of margin note column will be
% reduced. This can be done easily be swapping the two lengths.
% \changes{v3.30}{2020/02/25}{orphan line break at end of warning message
%   removed}
%    \begin{macrocode}
  \if@mincl
    \advance\textwidth by-\marginparwidth
    \advance\textwidth by-\marginparsep
    \ifdim\textwidth <\marginparwidth
      \PackageWarningNoLine{typearea}{%
        Width of text (\the\textwidth) less than width of\MessageBreak
        marginpar (\the\marginparwidth) while using option\MessageBreak
        `mpinclude'.\MessageBreak
        Because of this, I'm exchanging the values.\MessageBreak
        You should increase DIV to avoid such problems%
      }%
      \@tempskipa=\marginparwidth
      \marginparwidth=\textwidth
      \textwidth=\@tempskipa
    \fi
  \fi
%    \end{macrocode}
% Next the vertical size of a block.
%    \begin{macrocode}
  \ta@vblk=\paperheight
  \divide\ta@vblk by\ta@div
%    \end{macrocode}
% The top margin depends on this vertical block size. But note, \TeX{} will
% increase it by 1\,in.
%    \begin{macrocode}
  \topmargin=-1in
  \advance\topmargin by\ta@vblk
%    \end{macrocode}
% And if head is not part of the typing area, the top margin has to be reduced
% by the head size.
%    \begin{macrocode}
  \if@hincl \else
    \advance\topmargin by-\headheight
    \advance\topmargin by-\headsep
  \fi
%    \end{macrocode}
% For the height of the typing area we use the paper size without the height
% of head and footer. The head and footer may be part of the typing area or
% not. If they are part of the typing area they reduce the height of it.
%    \begin{macrocode}
  \ta@temp=\paperheight
  \advance\ta@temp by-3\ta@vblk
  \if@hincl
    \advance\ta@temp by-\headheight
    \advance\ta@temp by-\headsep
  \fi
  \if@fincl
    \advance\ta@temp by-\footskip
  \fi
%    \end{macrocode}
% Last but not least we have take care that the typing area consists of an
% integer number of text lines.
% \changes{v2.96}{2006/09/12}{setting \cs{topskip}}
% \changes{v2.97c}{2007/07/04}{setting \cs{maxdepth}}
%    \begin{macrocode}
  \scr@ifundefinedorrelax{@ptsize}{%
    \topskip=\f@size\p@
  }{%
    \topskip=\@ptsize\p@
    \advance\topskip by 10\p@
  }%
  \maxdepth=.5\topskip
  \textheight=\topskip
  \@whiledim\textheight <\ta@temp
  \do{\advance\textheight by\baselineskip}%
}
%    \end{macrocode}
% \end{macro}^^A \@typearea
%
% \begin{macro}{\ta@modiv}
% Needed by \cs{typearea}. The first parameter will be divided by the second
% one. \cs{ta@div} is set to the rounded result. Internally \cs{@tempdima} and
% \cs{@tempdimb} are used.
%    \begin{macrocode}
\newcommand*\ta@modiv[2]{
  \setlength{\@tempdima}{#1}
  \divide\@tempdima by#2
  \ta@div\@tempdima
  \ifnum \ta@div<4
    \ta@div=4
  \else
    \setlength{\@tempdimb}{#1}%
    \divide\@tempdimb by\ta@div
    \addtolength{\@tempdima}{\@tempdima}%
    \addtolength{\@tempdima}{-\@tempdimb}%
    \advance\ta@div by\@ne
    \setlength{\@tempdimb}{#1}%
    \divide\@tempdimb by\ta@div
%    \end{macrocode}
% \changes{v2.5a}{1997/06/22}{\cs{ifdim} instead of \cs{ifnum}}
%    \begin{macrocode}
    \ifdim \@tempdima<\@tempdimb
      \advance\ta@div by\m@ne
    \fi
  \fi
}
%    \end{macrocode}
% \end{macro}^^A \ta@modiv
%
% \begin{macro}{\ta@temp@goodwidth}
% Sets \cs{ta@temp} to the heuristic optimal line width depending on the
% current font. An optimal line width has 66 up to 72 characters. Here we use
% a value of 69 characters. We assume that such a line consists of 66 lower
% case letters (minors) and three upper case letters (majors). Normally the
% weight of a letter depends on the language but we use same weight for each
% letter.
% \changes{v2.7i}{2001/05/17}{avoid overflows if a very large font is used}
% \changes{v2.8q}{2002/03/06}{\cs{normalfont} \cs{normalsize} added}
% \changes{v2.95a}{2006/07/20}{fix of large font calculation}
%    \begin{macrocode}
\newcommand*\ta@temp@goodwidth{%
  \settowidth{\ta@temp}{\normalfont\normalsize 
    abcdefghijklmnopqrstuvwxyz}%
  \ifdim\ta@temp >200\p@
    \setlength{\ta@temp}{2.53846\ta@temp}%
    \settowidth{\@tempdima}{\normalfont\normalsize
      ABCDEFGHIJKLMNOPQRSTUVWXYZ}%
    \setlength{\@tempdima}{.11538\@tempdima}%
    \addtolength{\ta@temp}{\@tempdima}%
  \else
    \multiply\ta@temp by66
    \settowidth{\@tempdima}{\normalfont\normalsize
      ABCDEFGHIJKLMNOPQRSTUVWXYZ}%
    \multiply\@tempdima by3
    \addtolength{\ta@temp}{\@tempdima}%
    \divide\ta@temp by26
  \fi
}
%    \end{macrocode}
% \end{macro}^^A \ta@temp@goodwidth
%
% \begin{command}{\AfterCalculatingTypearea}
% \changes{v2.96a}{2006/11/29}{added}
% \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced by
%     \cs{kernel@ifstar}}
% Users can influence the behaviour of \cs{typearea} by a hook. The star
% variation adds the code locally the non-star variation globally.
%    \begin{macrocode}
\newcommand*{\AfterCalculatingTypearea}{%
  \kernel@ifstar \l@addto@macro \g@addto@macro \@typearea@end
}
%    \end{macrocode}
% \begin{macro}{\@typearea@end}
% \changes{v2.96a}{2006/11/29}{defined again but completely different}
% Macro \cs{@typearea@end} is a hook that is executed at the end of
% \cs{typearea} but before the activation of the new settings. So you can
% change the result of \cs{typearea}.
%    \begin{macrocode}
\newcommand*{\@typearea@end}{}
%    \end{macrocode}
% \end{macro}^^A \@typearea@end
% \end{command}^^A \AfterCalculatingTypearea
%
% \begin{command}{\activateareas}
% \changes{v2.96a}{2006/11/29}{new name for old macro}
% \changes{v3.17}{2015/02/07}{respect option \opt{usegeometry}}
% This macro is used to activate the new typing area and margin settings
% inside the document body. It is a hack, because is uses internal \LaTeX{}
% kernel code.
%    \begin{macrocode}
\newcommand*{\activateareas}{%
%    \end{macrocode}
% First of all test, whether or not we are inside the document body. If not do
% nothing.
%    \begin{macrocode}
  \if@atdocument
%    \end{macrocode}
% \changes{v3.23}{2017/04/03}{info instead of warning if used inside the
%     document body}
% But if, we show an info, because it is a hack. It depends in the \LaTeX{}
% release. Nevertheless, it seems to be stable from the very first \LaTeXe{}
% release. So we use an info only. Before v3.23 we have used a warning.
%    \begin{macrocode}
    \PackageInfo{typearea}{%
      Using dirty hack to recalculate internal output\MessageBreak
      dimensions of LaTeX%
    }%
%    \end{macrocode}
% Now, set several internal and none internal \LaTeX{}lengths to the new
% dimensions.
%    \begin{macrocode}
    \@colht=\textheight
    \@colroom=\textheight 
    \vsize=\textheight
    \columnwidth=\textwidth
    \if@twocolumn
      \advance\columnwidth by-\columnsep
      \divide\columnwidth by\tw@ 
      \hsize=\columnwidth
      \@firstcolumntrue
    \fi
    \hsize=\columnwidth 
    \linewidth\hsize
    \scr@ifundefinedorrelax{@pagesizelast}{}{%
      \KOMAoptions{pagesize=\@pagesizelast}%
    }%
%    \end{macrocode}
% \changes{v3.17}{2015/03/18}{adapt the \opt{parskip} settings}
% From v3.17 changes of the typing area can immediately change some of the
% \opt{parskip} settings. Before |\begin{document}| the change will be done
% automatically at |\begin{document}| and therefore adding it here is correct.
%    \begin{macrocode}
    \expandafter\ifnum\scr@ta@v@is@ge{3.17}%
      \csname par@updaterelative\endcsname
    \fi
  \fi
%    \end{macrocode}
% \changes{v3.17}{2015/02/07}{new code for option \opt{usegeometry}}
% If option \opt{usegeometry} is active we need to setup several options of
% package \pkg{geometry}. We use \cs{scr@geometryargs} to store them. And
% we either have to pass these options to \pkg{geometry} or define a new
% geometry. In the second case we also need some hacks to make the new paper
% size available for \pkg{geometry}.
%    \begin{macrocode}
  \edef\scr@geometryargs{%
    bindingoffset=\the\ta@bcor,
    lmargin=\the\dimexpr 1in+\oddsidemargin-\ta@bcor\relax,
    textwidth=\the\textwidth,
    \if@hincl
      includehead,%
      top=\the\dimexpr 1in+\topmargin\relax,
    \else
      includehead=false,%
      top=\the\dimexpr 1in+\topmargin+\headheight+\headsep\relax,
    \fi
    headheight=\the\headheight,
    headsep=\the\headsep,
    textheight=\the\textheight,
    \if@fincl
      includefoot,
    \else
      includefoot=false,
    \fi
    footskip=\the\footskip,
    \if@mincl
      includemp,
    \else
      includemp=false,
    \fi
    marginparwidth=\the\marginparwidth,
    marginparsep=\the\marginparsep
  }%
  \if@ta@usegeometry
    \@ifundefined{Gm@save}{%
      \if@atdocument\else
        \edef\scr@geometryargs{%
          paperwidth=\the\paperwidth,paperheight=\the\paperheight,%
          \scr@geometryargs
        }%
        \PassOptionsToPackage{\scr@geometryargs}{geometry}%
      \fi
    }{%
      \edef\Gm@restore@org{\Gm@restore}%
      \if@atdocument
        \expandafter\newgeometry\expandafter{\scr@geometryargs}%
      \else
        \edef\scr@geometryargs{%
          paperwidth=\the\paperwidth,paperheight=\the\paperheight,%
          \scr@geometryargs
        }%
        \expandafter\geometry\expandafter{\scr@geometryargs}%
      \fi
    }%
  \fi
}
%    \end{macrocode}
% \begin{macro}{\scr@geometryargs}
% \changes{v3.16}{2015/02/07}{neu (intern)}
% The initialization of the \pkg{geometry} options is empty. It will be
% filled automatically by \cs{activateareas}.
%    \begin{macrocode}
\newcommand*{\scr@geometryargs}{}
%    \end{macrocode}
% \end{macro}^^A \scr@geometryargs
% \end{command}^^A \activateareas
%
% \changes{v2.4g}{1996/11/04}{waste comments removed}
% \changes{v2.7g}{2001/04/17}{new symbolic values \opt{current},
%   \opt{default}, \opt{calc}, \opt{classic}}
% \changes{v2.8q}{2001/11/06}{new symbolic value \opt{last}}
% \changes{v2.96}{2006/08/21}{warn if \cs{typearea} is used inside a group}
% \changes{v3.10}{2011/09/13}{using \cs{scr@grouplevel@test}}
% \changes{v3.12}{2013/03/26}{page footer height configurable}
% \changes{v3.30}{2020/03/24}{\opt{current} after \cs{areaset} is \opt{areaset}}
%      
% The symbolic values of the obligatory argument of \cs{typearea}:
% \begin{center}\begin{tabular}{lll}
%     Symbol           & Value & Meaning \\\hline\\[-2ex]
%     \opt{calc}    & 2 & calculate a good value \\
%     \opt{classic} & 3 & \emph{mittelalterlicher Buchseitenkanon} \\
%     \opt{current} & --- & use the current value \\
%     \opt{default} & 0 & use a value from the list \\
%     \opt{last}    & --- & use the value from the last usage of
%     \cs{typearea} \\
%     \opt{areaset} & --- & replace the current typing area \\
% \end{tabular}\end{center}
%    
% There is only one symbolic value that can be used as the optional argument:
% \opt{current}.
%    \begin{macrocode}
\newcommand*\typearea[2][\z@]{%
  \scr@grouplevel@test\typearea
  \edef\@tempa{#1}\edef\@tempb{current}\ifx\@tempa\@tempb\else
    \setlength{\ta@bcor}{#1}%
  \fi
  \edef\@tempa{#2}%
  \def\@tempb{last}\ifx\@tempa\@tempb\let\@tempa=\@lastdiv
    \ifx\@tempa\relax \def\@tempa{current}\fi
  \fi
  \Ifstr{\@tempa}{current}{%
    \Ifstr{\@lastdiv}{areaset}{\let\@tempa\@lastdiv}{}%
  }{}%
  \def\@tempb{areaset}\ifx\@tempa\@tempb
    \areaset[current]{\textwidth}{\textheight}%
  \else
    \def\@tempb{current}\ifx\@tempa\@tempb
    \else
      \def\@tempb{default}\ifx\@tempa\@tempb\ta@div=\z@ \else
        \def\@tempb{calc}\ifx\@tempa\@tempb\ta@div=\tw@ \else
          \def\@tempb{classic}\ifx\@tempa\@tempb\ta@div=\thr@@ \else
            \ta@div=\@tempa
          \fi
        \fi
      \fi
    \fi
    \edef\@lastdiv{\the\ta@div}%
%    \end{macrocode}
% Before we do the calculation we need to start a new page if we are inside
% the document's body.
% \changes{v3.37}{2022/06/09}{fix for \LaTeX's build in two-column mode}%
% Unfortunately a simple \cs{newpage} maybe not enough, because this only
% starts a new page in \LaTeX's one-column mode. In \LaTeX's two-column mode
% it only starts a new column, which could be a new page or not.
% \begin{description}
% \item[ToDo:] Maybe a simple \cs{clearpage} would be better, but less
%   compatible with \KOMAScript{} before version 3.37.
% \end{description}
%    \begin{macrocode}
    \if@atdocument
      \newpage
      \if@twocolumn\ifnum\@colnum=\z@\else\null\newpage\fi\fi    
    \fi
%    \end{macrocode}
% \changes{v2.95a}{2006/07/20}{select document font size}%
% And we need to select the document font size.
%    \begin{macrocode}
    \normalsize
%    \end{macrocode}
% Is the height of the page head already given or do we need to calculate
% it. If we need to calculate it we use 1.25 of the base lines distance. This
% gives enough room for optional separation lines (which are set outside if
% use \pkg*{scrlayer-scrpage}).
% \changes{v2.4n}{1997/05/28}{page head height depends on base line
%     distance}
%    \begin{macrocode}
    \@ifundefined{ta@hh}{%
      \headheight=1.25\baselineskip
    }{%
      \headheight=\ta@hh
    }%
%    \end{macrocode}
% The distance between the head and the text is always the same. Different
% typographers recommend different values. A good value would result in a grid
% that would be used for all lines of the page. But the distance should not to
% large. Supposedly a good value would be the height of one text line. But
% with head separation rule this wouldn't be a good value. So we use a
% compromise.
%    \begin{macrocode}
    \headsep=1.5\baselineskip
%    \end{macrocode}
% The foot is a similar problem. But we have to setup also \len{footskip} not
% only \len{footheight}.
%    \begin{macrocode}
    \@ifundefined{footheight}{%
      \newlength\footheight
    }{}%
    \@ifundefined{ta@fh}{%
      \footheight=1.25\baselineskip
    }{%
      \footheight=\ta@fh
    }%
    \footskip=\dimexpr\footheight+2.25\baselineskip
%    \end{macrocode}
% Some values also independent from the divisor and the binding correction.
%    \begin{macrocode}
    \marginparsep=1cc
    \marginparpush=0.45\baselineskip
%    \end{macrocode}
% \changes{v2.4g}{1996/11/04}{\opt{DIV\quotechar=0} tries to use a value from the
%   default list}
% Before we do the calculation we test, whether the resulting divisor is
% 0. Values from 0 up to 3 are not possible because we use three parts for the
% margins. So we can use 0 to get a value from the default value list if
% available.
% \changes{v2.5h}{1999/12/29}{warning message changed}
%    \begin{macrocode}
    \ifnum\ta@div=\z@
      \@ifundefined{@ptsize}{%
        \@tempdima=\f@size\p@
        \advance\@tempdima by.5pt
        \ta@div=\@tempdima
        \divide\ta@div by65536
        \advance\ta@div by-10
        \xdef\@ptsize{\the\ta@div}%
        \PackageWarningNoLine{typearea}{%
          \string\@ptsize\space not defined!\MessageBreak
          Fontsize is \f@size pt.\MessageBreak
          Defining \string\@ptsize\space to \@ptsize%
        }%
      }{}%
      \ta@divfor{\@ptsize}%
    \fi
%    \end{macrocode}
% \changes{v2.4n}{1997/05/28}{optional \emph{klassischer Buchseitenkanon}}
% Another special case is if the divisor is 3. In this case we calculate a
% divisor near to the \emph{klassischer Buchseitenkanon}. This means we try to
% have the height of the typing area is as near as possible the width of the
% paper. For classic paper size this should be a harmonious result.
%    \begin{macrocode}
    \ifnum\ta@div=3
      \typeout{DIV calculation for classic typearea.}%
      \setlength{\ta@temp}{\paperwidth}%
      \addtolength{\ta@temp}{-\ta@bcor}%
%    \end{macrocode}
% Here we also need to take care for whether the head is part of the typing
% area or the margin.
%    \begin{macrocode}
      \if@hincl\else
        \addtolength{\ta@temp}{\headheight}%
        \addtolength{\ta@temp}{\headsep}%
      \fi
      \if@fincl\else
        \addtolength{\ta@temp}{\footskip}%
      \fi
%    \end{macrocode}
% Sometimes we need more vertical space than available. In this case we use
% another calculation. This will be defined later.
% \changes{v2.5a}{1997/06/22}{\cs{ifdim} instead of \cs{ifnum}}
%    \begin{macrocode}
      \ifdim \ta@temp >\paperheight
        \PackageWarningNoLine{typearea}{%
          No classic typearea with this papersize!\MessageBreak
          Using calculated DIV%
        }%
        \ta@div=\@ne
%    \end{macrocode}
% Otherwise we can calculate the height of all vertical margins by the height
% of the paper width (without binding correction)
%    \begin{macrocode}
      \else
        \setlength{\ta@temp}{\paperheight}%
        \addtolength{\ta@temp}{-\paperwidth}%
        \addtolength{\ta@temp}{\ta@bcor}%
%    \end{macrocode}
% and from this the height of one block
%    \begin{macrocode}
        \divide\ta@temp by3
%    \end{macrocode}
% and this will result in a divisor.
%    \begin{macrocode}
        \ta@modiv{\paperheight}{\ta@temp}%
      \fi
%    \end{macrocode}
% Here we do not need to respect \opt{mpinclude} because we do not use the
% width of the typing area but the height of the paper.
%    \begin{macrocode}
    \fi
%    \end{macrocode}
% If we did not found a good value until this point, we try to calculate the
% optimal divisor.
% \changes{v2.4n}{1997/05/28}{improved optimal divisor calculation}
%    \begin{macrocode}
    \ifnum\ta@div <4
      \typeout{DIV calculation for typearea with good linewidth.}%
%    \end{macrocode}
% The width of the typing area could at most be the width of the paper without
% the binding correction.
%    \begin{macrocode}
      \setlength{\textwidth}{\paperwidth}%
      \addtolength{\textwidth}{-\ta@bcor}%
%    \end{macrocode}
% The optimal width would be the optimal line width.
%    \begin{macrocode}
      \ta@temp@goodwidth
%    \end{macrocode}
% In two column mode each text line has two such columns plus the column
% separation width.
% \changes{v2.8q}{2001/12/13}{\opt{DIVcalc} uses wider columns in two
%     column mode}
%    \begin{macrocode}
      \if@twocolumn
        \addtolength{\ta@temp}{\ta@temp}%
        \addtolength{\ta@temp}{\columnsep}%
      \fi
%    \end{macrocode}
% Former releases used an iterative approach to get the best
% divisor. Depending on the \TeX{} implementation and the paper size this
% could be more or less slow. So we do a direct calculation starting with the
% width of the left and right margin.
%    \begin{macrocode}
      \setlength{\ta@hblk}{\paperwidth}%
      \addtolength{\ta@hblk}{-\ta@temp}%
%    \end{macrocode}
% This can be used to calculate the width of a block.
%    \begin{macrocode}
      \divide\ta@hblk by3
%    \end{macrocode}
% If this width is negative, we use a minimum width of 5\,mm.
% \changes{v2.5a}{1997/06/22}{\cs{ifdim} instead of \cs{ifnum}.}
%    \begin{macrocode}
      \ifdim \ta@hblk <\z@
        \setlength{\ta@hblk}{5mm}%
      \fi
%    \end{macrocode}
% The width of block and the width of the paper can be used to calculate the
% divisor.
% \changes{v2.8q}{2002/03/28}{respect \opt{mpinclude} at the calculation
%     of the best divisor}
% If option \opt{mpinclude} is active, we have to multiply the divisor with
% $\frac{3}{3+m}$. $m$ is the number of blocks used for the margin notes. If
% $m=1$ we have an factor of $\frac{3}{4}=0.75$. More difficult would be not
% to use a number of blocks for the margin note column but a dedicated value
% for the width of the margin note column.
%    \begin{macrocode}
      \if@mincl
        \setlength{\ta@temp}{.75\ta@hblk}%
      \else
        \setlength{\ta@temp}{\ta@hblk}%
      \fi
      \ta@modiv{\paperwidth}{\ta@temp}%
%    \end{macrocode}
% Another difficulty would be, if the divisor now is optimal for the width but
% result in a to large height. To find this out, we first calculate the typing
% area and margins with the found divisor.
%    \begin{macrocode}
      \@typearea
%    \end{macrocode}
% Now, we test the vertical sizes. Again a minimum block size is of 5\,mm will
% be used. But we have to take care the 1\,in of \TeX!
% \changes{v2.5a}{1997/06/22}{\cs{ifdim} instead of \cs{ifnum}}
%    \begin{macrocode}
      \setlength{\ta@temp}{-1in}%
      \addtolength{\ta@temp}{5mm}%
      \ifdim\topmargin <\ta@temp
%    \end{macrocode}
% This would be the minimum top margin. If the margin is less, we need another
% divisor. In this case we also have to recognize whether the page head and
% footer are part of the margins or the typing area.
% \changes{v2.5b}{1997/06/29}{take care for \opt{headinclude} and
%     \opt{footinclude} while calculating the divisor for minimum top
%     margin}
%    \begin{macrocode}
        \setlength{\ta@temp}{15mm}%
        \if@hincl\else
          \addtolength{\ta@temp}{\headheight}%
          \addtolength{\ta@temp}{\headsep}%
        \fi
        \if@fincl\else
          \addtolength{\ta@temp}{\footskip}%
        \fi
        \divide\ta@temp by3
        \ta@modiv{\paperheight}{\ta@temp}%
      \fi
%    \end{macrocode}
% A solution for this difficulty has been found.
%    \begin{macrocode}
    \fi
%    \end{macrocode}
% So now we have the final divisor and can do the final calculation.
%    \begin{macrocode}
    \@typearea
%    \end{macrocode}
% \changes{v2.0e}{1994/10/28}{write layout values to the \file{log} file}
% \changes{v2.4i}{1996/11/11}{write to the \file{log} file later}
% \changes{v2.4i}{1996/11/11}{execute \cs{@typearea@end} at the end of \cs{typearea}}
% \changes{v2.4j}{1996/12/04}{\file{log} output with only one
%   \cs{textheight} but additional \cs{baselineskip}}
% \changes{v2.5}{1997/06/17}{"`arithmetic overflow"' with \opt{landscape} fixed}
% \changes{v2.95}{2004/01/30}{warning in two column mode fixed}
% \changes{v3.00}{2008/10/08}{use \cs{ta@layoutinfo} to write the layout
%   values into the \file{log} file}
% \changes{v3.01b}{2008/12/09}{warning for sub-optimal settings changed}
% \changes{v3.26}{2018/07/16}{warning about long lines changed}
% \changes{v3.26}{2018/07/16}{warning about small lines changed and
%   replaced by an info}
% Last but not least we write some layout values into the \file{log} file. For
% some of the information we need additional calculations, e.g., of a good
% line width.
%    \begin{macrocode}
    \ta@temp@goodwidth
    \if@twocolumn
      \addtolength{\ta@temp}{\ta@temp}%
      \addtolength{\ta@temp}{\columnsep}%
    \fi
    \setlength{\@tempdima}{\ta@temp}%
    \addtolength{\ta@temp}{-\textwidth}%
    \divide\@tempdima by100
    \divide\ta@temp by\@tempdima
    \@tempcnta=\ta@temp
    \ifnum\@tempcnta <5
      \ifnum\@tempcnta >-5
        \@tempcnta=0
      \else
        \advance\@tempcnta by4
      \fi
    \else
      \advance\@tempcnta by-4
    \fi
    \ifnum\@tempcnta <-15
      \@tempcnta=-\@tempcnta
      \PackageWarningNoLine{typearea}{%
        Bad type area settings!\MessageBreak
        The detected line width is about
        \the\@tempcnta\@percentchar\MessageBreak
        larger than the heuristically estimated maximum\MessageBreak
        limit of typographical good line width.\MessageBreak
        You should e.g.
        \ifnum\ta@div >6 decrease DIV, \fi
        \ifdim\f@size\p@<\@xiipt\p@ increase fontsize\else
        (increase fontsize)\fi\MessageBreak
        or change papersize%
      }%
      \@tempcnta=-\@tempcnta
    \else
      \ifnum\@tempcnta>15
        \PackageInfoNoLine{typearea}{%
          Maybe no optimal type area settings!\MessageBreak
          The detected line width is about
          \the\@tempcnta\@percentchar\MessageBreak
          smaller than the heuristically estimated maximum\MessageBreak
          limit of typographical good line width.\MessageBreak
          You could e.g. increase DIV%
          \ifdim\f@size\p@>\@xpt\p@, decrease fontsize\fi\MessageBreak
          or change the papersize%
        }%
    \fi\fi
    \ifnum\ta@div <6
      \PackageWarningNoLine{typearea}{%
        Very low DIV value!\MessageBreak
        DIV values less than 6 result in textwidth/-height\MessageBreak
        smaller than total marginwidth/-height.\MessageBreak
        You should e.g. increase DIV, increase fontsize or\MessageBreak
        change papersize%
      }
    \fi
    \ta@layoutinfo{\number\ta@div}{\the\@tempcnta}%
    \KOMA@kav@removekey{.typearea.\scr@pkgextension}{DIV}%
    \KOMA@kav@xadd{.typearea.\scr@pkgextension}{DIV}{\@lastdiv}%
    \KOMA@kav@removekey{.typearea.\scr@pkgextension}{BCOR}%
    \KOMA@kav@xadd{.typearea.\scr@pkgextension}{BCOR}{\the\ta@bcor}%
    \@typearea@end
    \activateareas
    \ifdim \dimexpr 1in+\topmargin
                   +\headheight+\headsep
                   +\textheight
                   +\footskip+\dp\strutbox\relax
           >\paperheight
      \PackageWarningNoLine{typearea}{%
        page content and margins higher than paper.\MessageBreak
        This may result in page footer out of paper.\MessageBreak
        You should decrease DIV, decrease foot height,\MessageBreak
        or include footer into type area%
      }%
    \fi
  \fi
}
%    \end{macrocode}
%
% \begin{macro}{\ta@layoutinfo}
% \changes{v3.00}{2008/10/08}{added}
% Write several layout information into the \file{log} file. The first
% argument is the current divisor and the second the discrepancy from the
% optimal divisor or empty or \cs{relax}.
%    \begin{macrocode}
\newcommand*{\ta@layoutinfo}[2]{%
  \PackageInfo{typearea}{%
    These are the values describing the layout:\MessageBreak
    DIV\space\space=\space#1\MessageBreak
    BCOR\space=\space\the\ta@bcor\MessageBreak
    \string\paperwidth\space\space\space\space\space\space=%
                         \space\the\paperwidth\MessageBreak
    \space\string\textwidth\space\space\space\space\space\space=%
                         \space\the\textwidth\MessageBreak
    \ifx\relax#2\relax\else
      \space DIV departure\space\space\space=%
                           \space#2\@percentchar\MessageBreak
    \fi
    \space\string\evensidemargin\space=%
                         \space\the\evensidemargin\MessageBreak
    \space\string\oddsidemargin\space\space=%
                           \space\the\oddsidemargin\MessageBreak
    \string\paperheight\space\space\space\space\space=%
                         \space\the\paperheight\MessageBreak
    \space\string\textheight\space\space\space\space\space=%
                         \space\the\textheight\MessageBreak
    \space\string\topmargin\space\space\space\space\space\space=%
                         \space\the\topmargin\MessageBreak
    \space\string\headheight\space\space\space\space\space=%
                         \space\the\headheight\MessageBreak
    \space\string\headsep\space\space\space\space\space\space\space\space=%
                         \space\the\headsep\MessageBreak
    \space\string\topskip\space\space\space\space\space\space\space\space=%
                         \space\the\topskip\MessageBreak
    \space\string\footskip\space\space\space\space\space\space\space=%
                         \space\the\footskip\MessageBreak
    \space\string\baselineskip\space\space\space=%
                         \space\the\baselineskip\MessageBreak
  }%
}
%    \end{macrocode}
% \end{macro}^^A \ta@layoutinfo
% \end{command}^^A \typearea
%
% \begin{command}{\recalctypearea}
% \changes{v2.95}{2002/12/04}{added}
% This command can be used to recalculate the typing area and the margins with
% the divisor and the binding correction that has been used last. The command
% is available only after loading the package. So it is an indicator if we
% are in the load or the run-time phase of the package.
%    \begin{macrocode}
\AtEndOfPackage{\newcommand*{\recalctypearea}{\typearea[current]{last}}}
%</package&typearea&body>
%    \end{macrocode}
% \end{command}
%
% Initially \cs{ta@bcor} and \cs{ta@div} are zero if is has not been set using
% one of the options. The 0 divisor stands for using a value from the default
% list. So the final initialization of the typing area and margins is very
% easy.
% \changes{v2.8q}{2001/11/06}{initialization using symbolic values \opt{current}}
%    \begin{macrocode}
%<*package&typearea&final>
\typearea[current]{current}
%</package&typearea&final>
%    \end{macrocode}
%
% \subsection{Storage and reactivation of typing area and margin settings}
%
% \begin{command}{\storeareas}
% \changes{v3.10}{2011/09/13}{added}
% \changes{v3.18}{2015/05/20}{Hooks at the beginning and end of the defined macro}
% You can store the current settings of the typing area and the margins in a
% macro. If you use the macro later the stored settings will be restored.
% \begin{macro}{\scr@grouplevel@test}
% \changes{v3.10}{2011/09/13}{added}
% Warn if restoring the settings is used inside a group.
%    \begin{macrocode}
%<*package&typearea&body>
\newcommand*{\scr@grouplevel@test}[1]{%
  \ifcase\currentgrouptype\else
    \PackageWarning{typearea}{%
      \string#1\space used at group level
      \the\currentgrouplevel.\MessageBreak
      Using \string#1\space inside any group, e.g.\MessageBreak
      environments, math mode, boxes, etc. may result in\MessageBreak
      many type setting problems.\MessageBreak
      You should move the command \string#1\MessageBreak
      outside all groups%
    }%
  \fi
}
\newcommand*{\storeareas}[1]{%
  \edef\reserved@a{%
    \noexpand\newcommand*{\unexpanded{#1}}{%
      \noexpand\scr@grouplevel@test\unexpanded{#1}%
      \noexpand\csname scr@storeareas@before@hook\noexpand\endcsname
      \unexpanded\expandafter\expandafter\expandafter{%
        \csname scr@storeareas@before@current@hook\endcsname}%
      \noexpand\paperheight=\the\paperheight\space
      \noexpand\paperwidth=\the\paperwidth\space
      \noexpand\topmargin=\the\topmargin\space
      \noexpand\headheight=\the\headheight\space
      \noexpand\headsep=\the\headsep\space
      \noexpand\textheight=\the\textheight\space
      \noexpand\vsize=\the\vsize\space
      \noexpand\footskip=\the\footskip\space
      \noexpand\@colht=\the\@colht\space
      \noexpand\@colroom=\the\@colroom\space
      \noexpand\textwidth=\the\textwidth\space
      \noexpand\columnwidth=\the\columnwidth\space
      \noexpand\hsize=\the\hsize\space
      \noexpand\oddsidemargin=\the\oddsidemargin\space
      \noexpand\evensidemargin=\if@twoside
        \the\evensidemargin
      \else
        \the\oddsidemargin
      \fi\space
      \noexpand\linewidth=\the\linewidth\space
      \noexpand\maxdepth=\the\maxdepth\space
      \scr@ifundefinedorrelax{@pagesizelast}{}{%
        \noexpand\KOMAoptions{pagesize=\@pagesizelast}}%
      \noexpand\ta@div=\the\ta@div\space
      \noexpand\ta@bcor=\the\ta@bcor\space
      \noexpand\def\noexpand\@lastdiv{\@lastdiv}%
      \noexpand\marginparsep=\the\marginparsep\space
      \noexpand\marginparwidth=\the\marginparwidth\space
      \noexpand\def
      \expandafter\noexpand\csname scr@kav@KOMA.typearea.\scr@pkgextension\endcsname{%
        \unexpanded\expandafter\expandafter\expandafter{%
          \csname scr@kav@KOMA.typearea.\scr@pkgextension\endcsname}%
      }%
      \unexpanded\expandafter\expandafter\expandafter{%
        \csname scr@storeareas@after@current@hook\endcsname}%
      \noexpand\csname scr@storeareas@after@hook\noexpand\endcsname
    }%
  }%
  \reserved@a
}
%    \end{macrocode}
% \begin{command}{\BeforeRestoreareas,\AfterRestoreareas}
% \changes{v3.18}{2015/05/20}{added}
% \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced by \cs{kernel@ifstar}}
% \begin{macro}{\scr@storeareashook,\scr@storeareas@before@hook,
%               \scr@storeareas@before@current@hook,\scr@storeareas@after@hook,
%               \scr@storeareas@after@current@hook}
% \changes{v3.18}{2015/05/20}{added}
% Adds code to one of the hooks of the macros defined by \cs{storeareas}.
% The star variants adds code only to not yet defined macros. The non-star
% variant also changes already defined macros.
%    \begin{macrocode}
\newcommand*{\BeforeRestoreareas}{%
  \kernel@ifstar{\scr@storeareashook{before@current}}%
                {\scr@storeareashook{before}}%
}
\newcommand*{\AfterRestoreareas}{%
  \kernel@ifstar{\scr@storeareashook{after@current}}%
                {\scr@storeareashook{after}}%
}
\newcommand*{\scr@storeareashook}[2]{%
  \expandafter\l@addto@macro\csname scr@storeareas@#1@hook\endcsname{#2}%
}
\newcommand*{\scr@storeareas@before@hook}{}
\newcommand*{\scr@storeareas@before@current@hook}{}
\newcommand*{\scr@storeareas@after@hook}{}
\newcommand*{\scr@storeareas@after@current@hook}{}
%</package&typearea&body>
%    \end{macrocode}
% \end{macro}^^A \scr@storeareashook … \scr@storeareas@after@current@hook
% \end{command}^^A \BeforeRestoreareas,\AfterRestoreareas
% \end{macro}^^A \scr@grouplevel@test
% \end{command}^^A \storeareas
%
% \subsection{Calculation of margins for a given typing area}
%
% Beside the calculation of the typing area and the margins by a divisor
% \pkg*{typearea} also provides the calculation of margins for a given
% typing area.
%
% \begin{option}{areasetadvanced}
% \changes{v3.11}{2012/04/17}{added}
% Switch to advanced version of \cs{areaset}. This does calculate margins
% more like \cs{typearea}. Nevertheless, there's still a difference at the top
% margin, because \cs{typerea} doesn't balance differences in calculated
% height of margins and resulting height of margins but reduces only the
% bottom margin.
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@ifkey{areasetadvanced}{@areasetadvanced}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}
%
% \begin{command}{\areaset}
% \changes{v2.1b}{1994/12/31}{\cs{headheight} depends on \cs{@ptsize}}
% \changes{v2.6}{2000/01/04}{\cs{headheight} depends on \cs{f@size} instead
%   of \cs{@ptsize}}
% \changes{v2.7g}{2001/04/17}{symbolic value \opt{current} for the
%   optional argument}
% \changes{v2.95}{2002/12/05}{execute \cs{@typearea@end} at the end of
%   \cs{areaset}}
% If we have given width and height of the typing area we still can calculate
% the margins. Again we want to use this not only in the document preamble but
% also in the document body.
%    \begin{macrocode}
%<*package&typearea&body>
\newcommand*\areaset[3][\z@]{%
  \edef\@tempa{#1}\edef\@tempb{current}\ifx\@tempa\@tempb\else
    \setlength{\ta@bcor}{#1}%
  \fi
%    \end{macrocode}
% We have to start a new page if we are inside the document body.
%    \begin{macrocode}
  \if@atdocument\newpage\fi
%    \end{macrocode}
% Then we do the calculation.
% \changes{v2.96}{2006/09/12}{set \cs{topskip}}
% \changes{v3.00}{2008/10/08}{use \cs{ta@layoutinfo} to write layout
%     information into the \file{log} file}
%    \begin{macrocode}
  \setlength{\textwidth}{#2}%
  \setlength{\textheight}{#3}%
  \def\@lastdiv{areaset}%
  \KOMA@kav@removekey{.typearea.\scr@pkgextension}{DIV}%
  \KOMA@kav@add{.typearea.\scr@pkgextension}{DIV}{areaset}%
  \KOMA@kav@removekey{.typearea.\scr@pkgextension}{BCOR}%
  \KOMA@kav@add{.typearea.\scr@pkgextension}{BCOR}{\the\ta@bcor}%
  \ta@hblk=\paperwidth
  \advance\ta@hblk by-\ta@bcor
  \advance\ta@hblk by-\textwidth
%    \end{macrocode}
% \changes{v3.11}{2012/04/17}{advanced version with different margin widths}%
% With v3.11 we will have an advanced version of \cs{areaset}. This
% version takes care to option \opt{mpinclude}.
%    \begin{macrocode}
  \if@areasetadvanced
    \if@mincl
      \divide\ta@hblk by4
    \else
      \divide\ta@hblk by3
    \fi
  \else
    \divide\ta@hblk by3
  \fi
  \oddsidemargin=-1in
  \evensidemargin=-1in
  \if@twoside
    \if@semitwoside
      \advance\oddsidemargin by1.5\ta@hblk
      \advance\oddsidemargin by\ta@bcor
%    \end{macrocode}
% \changes{v3.12}{2013/10/15}{use alternating \opt{BCOR} with
%    \opt{twoside\quotechar=semi}}
% If \opt{twoside=semi} is used, form v3.12 we add the binding correction
% to the inner margin. If compatibility to version less than 3.12 has been
% selected the binding correction will be added to the left margin.
%    \begin{macrocode}
      \expandafter\ifnum\scr@ta@v@is@lt{3.12}\relax
        \evensidemargin=\oddsidemargin
      \else
        \advance\evensidemargin by1.5\ta@hblk
      \fi
    \else
      \advance\oddsidemargin by\ta@hblk
      \advance\oddsidemargin by\ta@bcor
      \advance\evensidemargin by2\ta@hblk
    \fi
  \else
    \advance\oddsidemargin by1.5\ta@hblk
    \advance\oddsidemargin by\ta@bcor
    \evensidemargin\oddsidemargin
  \fi
  \scr@ifundefinedorrelax{@ptsize}{%
    \topskip=\f@size\p@
  }{%
    \topskip=\@ptsize\p@
    \advance\topskip by 10\p@
  }%
%    \end{macrocode}
% \changes{v3.11}{2012/04/17}{advanced version with different margin widths}%
% With v3.11 we will have an advanced version of \cs{areaset}. This
% version uses the head height algorithm of \cs{typearea}.
%    \begin{macrocode}
  \if@areasetadvanced
    \normalsize
    \@ifundefined{ta@hh}{%
      \headheight=1.25\baselineskip
    }{%
      \headheight=\ta@hh
    }%
  \else
    \headheight=15\p@
    \advance\headheight by\f@size\p@
    \advance\headheight by-10\p@
  \fi
  \headsep=1.5\baselineskip
%    \end{macrocode}
% \changes{v3.12}{2013/03/26}{changes for height of foot}
%    \begin{macrocode}
  \@ifundefined{footheight}{\newlength\footheight}{}
  \if@areasetadvanced
    \@ifundefined{ta@fh}{%
      \footheight=1.25\baselineskip
      \footskip=3.5\baselineskip
    }{%
      \footheight=\ta@fh
      \footskip=\dimexpr \footheight+2.25\baselineskip\relax
    }%
  \else
    \footheight=1.25\baselineskip
    \footskip=3.5\baselineskip
  \fi
  \ta@vblk=\paperheight
  \advance\ta@vblk by-\textheight
  \divide\ta@vblk by3
  \if@hincl
    \advance\textheight by-\headheight
    \advance\textheight by-\headsep
  \fi
  \if@fincl
    \advance\textheight by-\footskip
  \fi
  \topmargin=-1in
  \advance\topmargin by\ta@vblk
  \if@hincl \else
    \advance\topmargin by-\headheight
    \advance\topmargin by-\headsep
  \fi
  \if@twoside
    \if@semitwoside
      \marginparwidth=\ta@hblk
    \else
      \marginparwidth=1.5\ta@hblk
    \fi
  \else
    \marginparwidth=\ta@hblk
  \fi
  \marginparsep=1cc
%    \end{macrocode}
% \changes{v3.11}{2012/04/17}{advanced version with different
%     \cs{marginparwidth} and unchanged \cs{marginparsep}}%
% With version 3.11 we will have an advanced version of \cs{areaset}. This
% version supports \opt{mpinclude} option like \cs{typearea}.
%    \begin{macrocode}
  \if@areasetadvanced
    \if@mincl
      \marginparwidth=\ta@hblk
      \if@twoside\if@semitwoside\else
          \advance\evensidemargin by\marginparwidth
      \fi\fi
      \advance\marginparwidth by-\marginparsep
    \fi
  \fi
  \marginparpush=0.45\baselineskip
  \ta@layoutinfo{areaset}{}%
%    \end{macrocode}
% \changes{v3.11}{2012/04/17}{using \cs{@areaset@end}}
% \changes{v3.16a}{2015/02/11}{using \cs{footheight} for calculation of
%     page usage}
% Since v3.11 we also support \cs{AfterSettingArea} analogous to
% \cs{AfterCalculatingTypearea}:
%    \begin{macrocode}
  \@areaset@end
  \activateareas
  \ifdim \dimexpr 1in+\topmargin
                 +\headheight+\headsep
                 +\textheight
                 +\footskip+\footheight-\ht\strutbox\relax
         >\paperheight
    \PackageWarningNoLine{typearea}{%
      page content and margins higher than paper.\MessageBreak
      This may result in page footer out of paper.\MessageBreak
      You should decrease DIV, decrease foot height,\MessageBreak
      or decreate height of text area%
    }%
  \fi
}
%    \end{macrocode}
% \end{command}
%
% \begin{command}{\AfterSettingArea}
% \changes{v3.11}{2012/04/17}{added}
% \changes{v3.20}{2016/04/12}{\cs{@ifstar} replaced by \cs{kernel@ifstar}}
% The user may influence the result of \cs{areaset}. The star version works
% local while the non-star version is global.
%    \begin{macrocode}
\newcommand*{\AfterSettingArea}{%
  \kernel@ifstar \l@addto@macro \g@addto@macro \@areaset@end
}
%    \end{macrocode}
% \begin{macro}{\@areaset@end}
% \changes{v3.11}{2012/04/17}{added}
% Macro \cs{@areaset@end} is a hook to make it possible to add some
% instructions to the end of the \cs{areaset} command. This will be executed
% before the activation of the settings inside a document.
%    \begin{macrocode}
\newcommand*{\@areaset@end}{}
%</package&typearea&body>
%    \end{macrocode}
% \end{macro}
% \end{command}
%
% \subsection{Deprecated options}
%
% Former versions of \KOMAScript{} had some options that are completely
% deprecated since \KOMAScript{} 3.00. Most of them but not all are pattern
% matching options.
%
% \begin{option}{DIVcalc,DIVclassic}
% \changes{v2.4g}{1996/11/04}{added}
% \changes{v2.95}{2002/12/04}{implemented using option \opt{DIV}}
% \changes{v2.97d}{2007/10/03}{\cs{PackageInfo} replaced by \cs{PackageInfoNoLine}}
% \changes{v3.01a}{2008/11/21}{deprecated}
% The former form of \opt{DIV=}\opt{calc} and \opt{DIV=}\opt{classic}.
%    \begin{macrocode}
%<*package&typearea&options>
\KOMA@DeclareDeprecatedOption[typearea]{DIVcalc}{DIV=calc}
\KOMA@DeclareDeprecatedOption[typearea]{DIVclassic}{DIV=classic}
%</package&typearea&options>
%    \end{macrocode}
% \end{option}^^A DIVcalc,DIVclassic
%
% \begin{option}{a...paper,b...paper,c...paper,d...paper}
% Formerly \opt{a}\meta{integer}\opt{paper},
% \opt{b}\meta{integer}\opt{paper},
% \opt{c}\meta{integer}\opt{paper},
% \opt{d}\meta{integer}\opt{paper} has been documented. Since
% \KOMAScript{} v3.00 most of them are deprecated and has been replaced by
% option \opt{paper}. Nevertheless, for compatibility these options and the
% commands needed to recognize and handle them are still implemented.
%
%
% \begin{macro}{\x@paper}
% \begin{macro}{\x@@paper}
% \changes{v2.5}{1997/06/17}{pattern matching of paper sizes}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% \changes{v3.28}{2019/11/18}{\cs{ifnumber} renamed to \cs{Ifnumber}}
% This macros will be needed when the paper size will be generated.
% \begin{command}{\isopaper}
% \changes{v2.5}{1997/06/17}{new}
% \changes{v2.95}{2002/12/03}{upper case letters are also allowed}
% \changes{v3.23}{2017/02/22}{using \cs{ISO@PaperNameToSize}}
% Because \cs{isopaper} is used inside the paper size detection by pattern
% matching to select a specific ISO/DIN paper size, it has to be defined
% before the pattern matching will be done.
%    \begin{macrocode}
%<*package&typearea&options>
\newcommand\isopaper[2][a]{%
%    \end{macrocode}
% The command has to parameters. The first one is optional and the basic size
% given by one of the letters:
% \begin{description}
% \item[a] -- ISO/DIN A sizes
% \item[b] -- ISO/DIN B sizes
% \item[c] -- ISO/DIN C sizes
% \item[d] -- ISO/DIN D sizes
% \end{description}
% The second and mandatory argument is the scale number
%    \begin{macrocode}
  \ISO@PaperNameToSize[typearea]#1#2\@nil
%    \end{macrocode}
% Select either landscape or portrait:
% \changes{v2.97e}{2007/10/23}{landscape or portrait}
% \changes{v3.22}{2016/07/29}{adapting paper orientation}
%    \begin{macrocode}
  \expandafter\ifnum\scr@ta@v@is@ge{3.22}\relax
    \scr@setpaperorientation
  \else
    \if@landscape\scr@switchpapersizes\fi
  \fi
}
%    \end{macrocode}
% \pkg*{typearea} uses its own error message.
% \begin{macro}{\scr@UnknownPaperSize@typearea}
% \changes{v3.23}{2017/02/22}{new}
%    \begin{macrocode}
\ProvideUnknownPaperSizeError{typearea}{%
  \PackageError{typearea}{%
    Unknown ISO/DIN-papersize-letter #1%
  }{%
    There are only four defined paper scalings:\MessageBreak
    a = ISO/DIN-A, b = ISO/DIN-B, c = ISO/DIN-C, 
    d = ISO/DIN-D\MessageBreak
    Read KOMA-Script-documentation for further information.%
  }%
  \setlength{\paperwidth}{841sp}%
  \setlength{\paperheight}{1189sp}%
}
%    \end{macrocode}
% \end{macro}^^A \scr@UnknownPaperSize@typearea
% \end{command}^^A \isopaper
% \begin{macro}{\scr@setpaperorientation}
% \changes{v3.22}{2016/07/29}{new}
% Setup the paper orientation depending on \cs{if@landscape}.
%    \begin{macrocode}
\newcommand*\scr@setpaperorientation{%
  \ifdim\paperheight>\paperwidth
    \if@landscape\scr@switchpapersizes\fi
  \else
    \if@landscape\else\scr@switchpapersizes\fi
  \fi
}
%    \end{macrocode}
% \begin{macro}{\scr@switchpapersizes}
% \changes{v3.22}{2016/07/29}{new}
% Switches the two lengths \len{paperwidth} and \len{paperheight}.
%    \begin{macrocode}
\newcommand*\scr@switchpapersizes{%
  \@tempdima\paperwidth\paperwidth\paperheight\paperheight\@tempdima
}
%    \end{macrocode}
% \end{macro}^^A \scr@switchlength
% \end{macro}^^A \scr@setpaperorientation
% \begin{macro}{\iso@paper}
% \changes{v2.95}{2002/12/03}{new}
% Similar to \cs{isopaper} but with only one argument for the base size and
% the scale number.
% \begin{macro}{\iso@@paper}
% \changes{v2.95}{2002/12/03}{new}
% \changes{v3.28}{2019/11/18}{\cs{ifnumber} renamed to \cs{Ifnumber}}
% The single argument is split into base size letter and scale number by
% pattern matching.
%    \begin{macrocode}
\newcommand*{\iso@paper}[1]{\expandafter\iso@@paper#1\@nil}
\newcommand*{\iso@@paper}{}
\def\iso@@paper#1#2\@nil{%
  \@tempswafalse
  \if #1a\@tempswatrue
  \else\if #1A\@tempswatrue
    \else\if #1b\@tempswatrue
      \else\if #1B\@tempswatrue
        \else\if #1c\@tempswatrue
          \else\if #1C\@tempswatrue
            \else\if #1d\@tempswatrue
              \else\if #1D\@tempswatrue
  \fi\fi\fi\fi\fi\fi\fi\fi
  \if@tempswa
    \Ifnumber{#2}{\isopaper[#1]{#2}}{\@tempswafalse}%
  \fi
}
%</package&typearea&options>
%    \end{macrocode}
% \end{macro}^^A \iso@@paper
% \end{macro}^^A \iso@paper
%
% \begin{option}{DIV...}
% \begin{option}{BCOR...}
% Formerly \opt{DIV}\meta{integer}, \opt{BCOR}\meta{length},
% \opt{headlines}\meta{real} has been documented as options. Since
% \KOMAScript{} 3.00 these have been replaced by \opt{DIV=}\meta{integer},
% \opt{BCOR=}\meta{length} and
% \opt{headlines=}\meta{real}. Nevertheless, for compatibility the
% deprecated options are still handled by \cs{DeclareOption*} and so we
% need several helper macros to recognize them.
%
%
% \begin{macro}{\@BCOR}
% \begin{macro}{\@@BCOR}
% \changes{v2.5}{1997/06/17}{Single letter scanner replaced by pattern matching}
% \changes{v3.28}{2019/11/18}{\cs{ifstr} renamed to \cs{Ifstr}}
% Macro \cs{@BCOR} it needed to handle option \opt{BCOR\emph{BCOR}}. The
% option is recognized by pattern matching.
%    \begin{macrocode}
%<*(class|(package&typearea))&options>
\newcommand*{\@BCOR}{\expandafter\@@BCOR\CurrentOption BCORBCOR\@@BCOR}
\newcommand*{\@@BCOR}{}
\def\@@BCOR #1BCOR#2BCOR#3\@@BCOR{%
%    \end{macrocode}
% If it is option \opt{BCOR} the first argument is empty.
%    \begin{macrocode}
  \Ifstr{#1}{}{%
%    \end{macrocode}
% Additionally the third argument is |BCOR|.
%    \begin{macrocode}
    \Ifstr{#3}{BCOR}{%
%    \end{macrocode}
% Now, the second argument should be the size. If this is not the case, an
% error has to be reported.
% \changes{v3.22}{2016/07/29}{missing deprecated option warning added}
% \changes{v3.28}{2019/11/18}{\cs{ifdimen} renamed to \cs{Ifdimen}}
% \changes{v3.30}{2020/03/24}{avoid error message for unknown \opt{BCOR}
%   value to \cs{documentclass}}
% \changes{v3.31}{2020/04/27}{improved valid value recognization}
%    \begin{macrocode}
%<*class>  
      \Ifstrstart{#2}{=}{%
        \edef\reserved@a{\noexpand\Ifdimen{\@gobble#2}}%
        \reserved@a{\PassOptionsToPackage{BCOR#2}{typearea}}{\OptionNotUsed}%
      }{%
        \Ifdimen{#2}{\PassOptionsToPackage{BCOR#2}{typearea}}{\OptionNotUsed}%
      }%
%</class>
%<*package>
      \Ifdimen{#2}{%
        \KOMA@UseDeprecatedOption{%
          \PackageWarningNoLine{typearea}}{BCOR#2}{BCOR=#2}%
      }{\scr@pti@nerr@r}%
%</package>
%    \end{macrocode}
% In all other cases it is not this option. So we report an error. Instead of
% the error another option could be tested.
%    \begin{macrocode}
    }{\scr@pti@nerr@r}%
  }{\scr@pti@nerr@r}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\@DIV}
% \begin{macro}{\@@DIV}
% \changes{v2.5}{1997/06/17}{Single letter scanner replaced by pattern matching}
% \changes{v3.22}{2016/07/29}{missing deprecated option warning added}
% \changes{v3.28}{2019/11/18}{\cs{ifnumber} renamed to \cs{Ifnumber}}
% \changes{v3.30}{2020/03/24}{avoid error message for unknown \opt{DIV}
%   value to \cs{documentclass}}
% \changes{v3.31}{2020/04/27}{improved valid value recognization}
% Here we use more or less the same functionality as with \cs{@BCOR} and \cs{@@BCOR}.
%    \begin{macrocode}
\newcommand*{\@DIV}{\expandafter\@@DIV\CurrentOption DIVDIV\@@DIV}
\newcommand*{\@@DIV}{}
\def\@@DIV #1DIV#2DIV#3\@@DIV{%
  \edef\@tempa{#1}\ifx\@tempa\@empty
    \edef\@tempa{#3}\def\@tempb{DIV}\ifx\@tempa\@tempb
%<*class>  
      \Ifstrstart{#2}{=}{%
        \edef\reserved@a{\noexpand\Ifnumber{\@gobble#2}}%
        \reserved@a{\PassOptionsToPackage{DIV#2}{typearea}}{\OptionNotUsed}%
      }{%
        \Ifnumber{#2}{\PassOptionsToPackage{DIV#2}{typearea}}{\OptionNotUsed}%
      }%
%</class>
%<*package>
      \Ifnumber{#2}{%
        \KOMA@UseDeprecatedOption{%
          \PackageWarningNoLine{typearea}}{DIV#2}{DIV=#2}%
      }{\scr@pti@nerr@r}%
%</package>
    \else\scr@pti@nerr@r\fi
%    \end{macrocode}
% The only difference is, that we do not report an error but also test for
% \opt{BCOR}.
%    \begin{macrocode}
  \else\@BCOR\fi
}
%    \end{macrocode}
% \end{macro}^^A \@@DIV
% \end{macro}^^A \@DIV
%
% \begin{option}{headlines...}
% \begin{macro}{\@headlines}
% \begin{macro}{\@@headlines}
% \changes{v2.7g}{2001/04/17}{neW pattern matching for the number of headlines}
% The macros are needed to recognize the deprecated option
% \opt{headlines\meta{integer}}.
%    \begin{macrocode}
\newcommand{\@headlines}{%
  \expandafter\@@headlines\CurrentOption headlines\@@headlines%
}
\newcommand*{\@@headlines}{}
\def\@@headlines #1headlines#2\@@headlines{
  \edef\@tempa{#2}\edef\@tempb{headlines}\ifx\@tempa\@tempb
%<class>    \PassOptionsToPackage{#1headlines}{typearea}
%<package>    \setlength{\@tempdima}{#1\baselineskip}%
%<package>    \def\ta@hh{#1\baselineskip}%
  \else\@DIV\fi
}
%</(class|(package&typearea))&options>
%    \end{macrocode}
% \end{macro}^^A \@headlines
% \end{macro}^^A \@@headlines
%
% Starting point for all the deprecated pattern matching options is
% \cs{DeclareOption*} for command \cs{x@paper} that uses pattern
% matching with \cs{x@@paper} to recognize the deprecated options. These
% are needed not only for package \pkg*{typearea} but also for the classes.
%
% Back to the pattern matching. This one is the most simple of all.
% \changes{v2.97e}{2007/10/23}{passing options \pkg*{typearea} using the new syntax}
%    \begin{macrocode}
%<*(class|(package&typearea))&options>
\newcommand*{\x@paper}{%
  \expandafter\x@@paper\CurrentOption paper\x@@paper%
}
\newcommand*{\x@@paper}{}
\def\x@@paper #1#2paper#3\x@@paper{%
  \Ifstr{#3}{paper}{%
    \Ifnumber{#2}{%
      \Ifstr{#1}{a}{%
%<class>        \PassOptionsToPackage{#3=#1#2}{typearea}%
%<package>        \isopaper[a]{#2}%
      }{%
        \Ifstr{#1}{b}{%
%<class>          \PassOptionsToPackage{#3=#1#2}{typearea}%
%<package>          \isopaper[b]{#2}%
        }{%
          \Ifstr{#1}{c}{%
%<class>            \PassOptionsToPackage{#3=#1#2}{typearea}%
%<package>            \isopaper[c]{#2}%
          }{%
            \Ifstr{#1}{d}{%
%<class>              \PassOptionsToPackage{#3=#1#2}{typearea}%
%<package>              \isopaper[d]{#2}%
            }{\scr@pti@nerr@r}%
          }%
        }%
      }%
    }{\scr@pti@nerr@r}%
  }{%
%<class>    \@pt@scan
%<package>    \@headlines
  }%
}
%    \end{macrocode}
%
% \begin{macro}{\scr@pti@nerr@r}
% \changes{v2.95}{2002/06/20}{\cs{OptionNotUsed} instead of \cs{PackageError}} 
% If the addition option is neither \opt{BCOR\dots} nor \opt{DIV\dots} nor
% \opt{headlines} nor a known paper size, it is still unused.
%    \begin{macrocode}
\newcommand*{\scr@pti@nerr@r}{
%<class>  \OptionNotUsed
%<package>  \@unknownoptionerror
}
%    \end{macrocode}
% \end{macro}
%
% \changes{v2.97c}{2007/09/12}{\cs{lco@test} added to before pattern matching
%   of options}
%    \begin{macrocode}
\DeclareOption*{%
%<letter>  \lco@test
  \x@paper}
%</(class|(package&typearea))&options>
%    \end{macrocode}
% \end{option}^^A headline...
% \end{option}^^A DIV...
% \end{option}^^A BCOR...
% \end{macro}^^A \x@paper
% \end{macro}^^A \x@@paper
% \end{option}^^A a...paper … d...paper
%
%
% ^^A This part should stay at the end of the source file!
% \subsection{Configuration file}
%
% At the very ending of the option section just before executing and
% processing the options you can add additional options the
% \pkg*{typearea}. This is a very seldom used feature and I recommend to
% not use it any longer. If you need additional options use
% |\BeforeFamilyProcessOptions[.typearea.sty]{KOMA}{...}|.
%
% \changes{v2.0e}{1994/10/28}{configuration file \file{typearea.cfg}}
%    \begin{macrocode}
%<*package&typearea&options>
\InputIfFileExists{typearea.cfg}{%
  \typeout{%
    ***************************************^^J%
    * Local config file typearea.cfg used^^J%
    ***************************************%
  }
}{}
%</package&typearea&options>
%    \end{macrocode}
%
% \subsection{Option execution}
%
% The paper size and telling the back-end driver information about the paper
% size are default. The default for the default paper size will be \opt{A4}
% if nothing else was defined by a class. Execution of option \opt{twoside}
% depends already on the setting of \cs{if@twoside} and
% \cs{if@semitwoside}. This may be surprising but nevertheless is correct.
% \changes{v2.3h}{1995/01/19}{\cs{relax} after \cs{ProcessOptions} added}
% \changes{v2.96}{2006/08/18}{processing options in order of usage instead of
%   order of declaration}
% \changes{v2.97c}{2007/04/19}{Usage of \cs{KOMAExecuteOptions} instead of
%   \cs{KOMAoptions}}
% \changes{v2.97c}{2007/04/19}{Usage of \cs{KOMAProcessOptions} instead of
%   \cs{ProcessOptions}}
% \changes{v2.97c}{2007/04/19}{set only options that are not an implicit
%   default}
% \changes{v2.97c}{2007/07/18}{explicit setting of a valid value to
%   \opt{twoside}}
% \changes{v3.12}{2013/11/11}{the default paper size is given by
%   \cs{defaultpagesize}}
% \changes{v3.17}{2015/03/02}{\opt{pagesize} is default}
% \begin{macro}{\defaultpapersize}
% \changes{v3.12}{2013/11/11}{added}
% The default paper size. Defined using \cs{providecommand} so it can be
% changed by a class or wrapper package or \file{typearea.cfg} or somewhere
% else before loading \pkg*{typearea}.
%    \begin{macrocode}
%<*package&typearea&options>
\providecommand*{\defaultpapersize}{A4}
%</package&typearea&options>
%    \end{macrocode}
% \end{macro}
%    \begin{macrocode}
%<*package&typearea&options>
\KOMAExecuteOptions{paper=\defaultpapersize,pagesize}
\if@twoside
  \if@semitwoside
    \KOMAExecuteOptions{twoside=semi}
  \else
    \KOMAExecuteOptions{twoside}
  \fi
\else
  \KOMAExecuteOptions{twoside=false}
\fi
\KOMAProcessOptions\relax
%</package&typearea&options>
%    \end{macrocode}
%
% \Finale
% \PrintChanges
% 
\endinput
% Local Variables:
% mode: doctex
% ispell-local-dictionary: "en_US"
% eval: (flyspell-mode 1)
% TeX-master: t
% TeX-engine: luatex-dev
% eval: (setcar (or (cl-member "Index" (setq-local TeX-command-list (copy-alist TeX-command-list)) :key #'car :test #'string-equal) (setq-local TeX-command-list (cons nil TeX-command-list))) '("Index" "mkindex %s" TeX-run-index nil t :help "makeindex for dtx"))
% End:
