%%
%% This is file `flexipage.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% flexipage.dtx  (with options: `package')
%% 
%%  Copyright (C) 2022 by Anton Vrba
%%  --------------------------------
%% 
%%  This work may be distributed and/or modified under the
%%  conditions of the LaTeX Project Public License, either version 1.3
%%  of this license or (at your option) any later version.
%%  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 2008-05-04 or later.
%% 
%% 
%%  This work has the LPPL maintenance status `maintained'.
%% 
%%  The Current Maintainer of this work is Anton Vrba.
%% 
%% 
\NeedsTeXFormat{LaTeX2e}[2020/10/01]
\ProvidesPackage{flexipage}
    [2023/03/17 1.02 Fixed bug re fixed page sizes]


\RequirePackage{xparse}   % new LaTeX3 syntax to define macros and environments
\RequirePackage{l3keys2e} % nice way for option processing
\RequirePackage{calc}     % length manipulation
\RequirePackage{fp}       % floating point calculation
\RequirePackage{ifoddpage}
\RequirePackage{graphics}  % provides landscaping
\RequirePackage{mparhack} % not needed here, but the user certainly needs it.
\RequirePackage{etoolbox} % provides \patchcmd
\newlength{\fleximarginparsep}
\newlength{\flx@paperheight}
\newlength{\flx@paperwidth}
\newlength{\flx@temp}
\newlength{\flx@LSfix}
\setlength{\flx@LSfix}{0pt}
\newlength{\flx@lsfix}
\setlength{\flx@lsfix}{0pt}
\newlength{\flx@textheigt}
\newlength{\flx@orgtextwidth}
\newlength{\flx@oldparsep}
\newlength{\flx@oparsep}
\newlength{\flx@binding}
\newlength{\marginparsepodd}
\newlength{\marginparsepeven}
\newlength{\flx@inner}
\newlength{\flx@outer}
\newlength{\flx@top}
\newlength{\flx@bot}
\newlength{\flx@parsep}
\newlength{\flx@printarea}
\newlength{\flx@outerbleed}
\newlength{\flx@innerbleed}
\newlength{\flx@extrafirstfoot}
\setlength{\flx@extrafirstfoot} {0pt}
\setlength{\flx@binding}  {9pt}
\setlength{\flx@inner} {36pt}
\setlength{\flx@outer} {36pt}
\setlength{\flx@top}   {36pt}
\setlength{\flx@bot}   {45pt}
\setlength{\flx@parsep}{3ex}
\setlength{\flx@outerbleed} {0pt}
\setlength{\flx@innerbleed} {0pt}
\FPset\flx@m{3}
\FPset\flx@g{12}
\newif\ifSF@frame\SF@framefalse
\newif\ifFix@Margin\Fix@Margintrue
\newif\ifUse@fancyhdr\Use@fancyhdrtrue
\setlength{\flx@paperheight}{\paperheight}
\setlength{\flx@paperwidth}{\paperwidth}

\NewDocumentCommand \flx@setpapersize {o o m}{
\setlength{\flx@paperheight}{#2#3}
\setlength{\flx@paperwidth}{#1#3}
}

\ExplSyntaxOn
\keys_define:nn { flexiprint }
{
        binding .code:n = \setlength{\flx@binding}{#1},
          inner .code:n = \setlength{\flx@inner}{#1},
          outer .code:n = \setlength{\flx@outer}{#1},
            top .code:n = \setlength{\flx@top}{#1},
            bot .code:n = \setlength{\flx@bot}{#1},
   marginparsep .code:n = \setlength{\flx@parsep}{#1},
    paperheight .code:n = \setlength{\flx@paperheight}{#1},
     paperwidth .code:n = \setlength{\flx@paperwidth}{#1},
     outerbleed .code:n = \setlength{\flx@outerbleed}{#1},
     innerbleed .code:n = \setlength{\flx@innerbleed}{#1},
           grid .code:n = \FPset\flx@g{#1},
   marginalgrid .code:n = \FPset\flx@m{#1} ,
           show .code:n = \SF@frametrue ,
    NoFixMargin .code:n = \Fix@Marginfalse ,
        headsep .code:n = \setlength{\headsep}{#1},
     headheight .code:n = \setlength{\headheight}{#1},
       footskip .code:n = \setlength{\footskip}{#1},
 extrafirstfoot .code:n = \setlength{\flx@extrafirstfoot}{#1},
    fancyhdrOff .code:n = \Use@fancyhdrfalse,
        a4paper .code:n = \flx@setpapersize[210][297]{mm},
        a5paper .code:n = \flx@setpapersize[148][210]{mm},
        b4paper .code:n = \flx@setpapersize[250][353]{mm},
        b5paper .code:n = \flx@setpapersize[176][250]{mm},
        c4paper .code:n = \flx@setpapersize[229][324]{mm},
        c5paper .code:n = \flx@setpapersize[162][229]{mm},
       b4jpaper .code:n = \flx@setpapersize[257][364]{mm},
       b5jpaper .code:n = \flx@setpapersize[182][257]{mm},
    letterpaper .code:n = \flx@setpapersize[8.5][11]{in},
     legalpaper .code:n = \flx@setpapersize[8.5][14]{in},
 executivepaper .code:n = \flx@setpapersize[7.25][10.5]{in},
      10x8paper .code:n = \flx@setpapersize[8][10]{in},
       US-Trade .code:n = \flx@setpapersize[6][9]{in},
    UK-B-Format .code:n = \flx@setpapersize[135][210]{mm},
    US-B-Format .code:n = \flx@setpapersize[129][198]{mm},
       UK-Royal .code:n = \flx@setpapersize[156][234]{mm},
         Quarto .code:n = \flx@setpapersize[9.5][12]{in},
         Octavo .code:n = \flx@setpapersize[6][9]{in},
    CrownQuarto .code:n = \flx@setpapersize[189][246]{mm},
           Demi .code:n = \flx@setpapersize[138][216]{mm},
}
\ProcessKeysOptions { flexiprint }

\DeclareExpandableDocumentCommand{\IfNoValueOrEmptyTF}{ m m m }
{
 \IfNoValueTF{#1}
  {#2}
  {\tl_if_empty:nTF {#1} {#2} {#3}}
}
\ExplSyntaxOff

\FPset\flx@oldg{\flx@g}
\FPset\flx@oldm{\flx@m}
\FPset\flx@og{\flx@g}
\FPset\flx@om{\flx@m}
\setlength{\flx@oparsep}{\flx@parsep}
\ifUse@fancyhdr
   \RequirePackage{fancyhdr}
   \pagestyle{fancy}
\fi

\setlength{\flx@oldparsep}{\flx@parsep}
\setlength{\flx@oparsep}{\flx@parsep}
\addtolength{\flx@binding}{\flx@innerbleed}
\global\hoffset=0pt
\global\voffset=0pt

\NewDocumentCommand \flx@setpagevertical {} {
  \setlength{\flx@temp}{\flx@paperwidth+\flx@innerbleed+\flx@outerbleed}
  \global\paperwidth=\flx@temp

  \setlength{\flx@temp}{\flx@paperheight+2\flx@outerbleed}
  \global\paperheight=\flx@temp

  \setlength{\flx@temp}{\flx@top+\flx@outerbleed-72pt}
  \setlength{\topmargin}{\flx@temp}

  \setlength{\flx@temp}{\paperheight - \flx@top %
            - \headheight - \headsep -\footskip - \flx@bot - 2\flx@outerbleed}
  \setlength{\flx@textheigt}{\flx@temp}
  \setlength{\textheight}{\flx@textheigt-\flx@extrafirstfoot}
}
\flx@setpagevertical
\AddToHook{shipout/firstpage}{%
\global\textheight=\flx@textheigt
}

\newlength{\flx@linewidth}

\NewDocumentCommand \flx@setpage {} {
    \FPifgt\flx@m0\relax\else\setlength{\flx@parsep}{0pt}\fi
    \setlength{\flx@temp}{\flx@binding+\flx@inner-72pt}
    \global\oddsidemargin=\flx@temp

    \setlength{\flx@linewidth}{\paperwidth-\oddsidemargin-\flx@outer-72pt-\flx@outerbleed}
    \ifUse@fancyhdr
       \setlength{\headwidth}{\flx@linewidth}
    \fi
    \setlength{\flx@printarea}{\flx@linewidth-\flx@parsep}
    \FPsub\flx@t\flx@g\flx@m
    \setlength{\flx@temp}{\flx@printarea * \real{\flx@t}/\real{\flx@g} }
    \global\textwidth=\flx@temp

    \setlength{\flx@temp}{\flx@printarea * \real{\flx@m}/\real{\flx@g} }
    \global\marginparwidth=\flx@temp
    \global\marginparsep=\flx@parsep
    \if@twoside
      \setlength{\flx@temp}{\flx@outer+\marginparwidth+\marginparsep-1in+\flx@outerbleed}
      \global\evensidemargin=\flx@temp
    \else
      \global\evensidemargin=\oddsidemargin
    \fi
    \global\fleximarginparsep=\marginparsep
    \global\hsize=\textwidth
}
\flx@setpage
\flx@orgtextwidth=\textwidth
\FPset\flx@oldg\flx@g
\FPset\flx@oldm\flx@m
\setlength{\flx@oparsep}{\flx@parsep}
\setlength{\flx@oldparsep}{\flx@parsep}
\NewDocumentCommand \NewMarginPage {o d<> m} {
   \FPset\flx@oldg\flx@g
   \FPset\flx@oldm\flx@m
   \setlength{\flx@oldparsep}{\flx@parsep}
   \ResetFlexiPage
   \IfNoValueOrEmptyTF{#2}{\relax}{\setlength{\flx@parsep}{#2} }
   \IfNoValueOrEmptyTF{#1}{\relax}{\FPset\flx@g{#1}}
   \FPset\flx@m{#3}
   \flx@setpage
   \flx@marginparcorrection
}
\NewDocumentCommand \OldMarginPage {} {
   \FPset\flx@m\flx@oldm
   \FPset\flx@g\flx@oldg
   \flx@ResetFlexiPage
   \setlength{\flx@parsep}{\flx@oldparsep}
   \flx@setpage
   \flx@marginparcorrection
}
\NewDocumentCommand \NewFullPage {}{
   \FPset\flx@oldg\flx@g
   \FPset\flx@oldm\flx@m
   \setlength{\flx@oldparsep}{\flx@parsep}
   \flx@ResetFlexiPage
   \FPset\flx@m{0}
   \setlength{\flx@parsep}{0pt}
   \flx@setpage
}
\NewDocumentCommand \ResetFlexiPage{} {
   \FPset\flx@oldg\flx@g
   \FPset\flx@oldm\flx@m
   \flx@ResetFlexiPage
}
\NewDocumentCommand \flx@ResetFlexiPage{} {
   \clearpage
   \FPset\flx@g\flx@og
   \FPset\flx@m\flx@om
   \setlength{\flx@parsep}{\flx@oparsep}
   \flx@setpage
   \flx@marginparcorrection
}
\NewDocumentCommand \Landscape {}
{
   \FPset\flx@oldm\flx@m
   \FPset\flx@oldg\flx@g
   \setlength{\flx@oldparsep}{\flx@parsep}
  \clearpage
  \setlength{\flx@LSfix}{\textheight-\flx@linewidth}
  \FPset\flx@m{0}
  \setlength{\flx@parsep}{0ex}
  \flx@setpage
  \flx@landscape
}
\NewDocumentCommand \endLandscape {}
{
   \flx@endlandscape
   \FPset\flx@m\flx@oldm
   \FPset\flx@g\flx@oldg
   \setlength{\flx@parsep}{\flx@oldparsep}
   \flx@setpage
   \setlength{\flx@LSfix}{0pt}
   \flx@marginparcorrection
}
\NewDocumentCommand \landscape {}
{
   \FPset\flx@oldm\flx@m
   \FPset\flx@oldg\flx@g
   \setlength{\flx@oldparsep}{\flx@parsep}
  \clearpage
  \setlength{\flx@LSfix}{\textheight-\textwidth}
  \FPset\flx@m{0}
  \setlength{\flx@parsep}{0ex}
  \flx@landscape
}
\NewDocumentCommand \endlandscape {}
{
   \flx@endlandscape
   \FPset\flx@m\flx@oldm
   \FPset\flx@g\flx@oldg
   \setlength{\flx@parsep}{\flx@oldparsep}
   \flx@setpage
   \setlength{\flx@LSfix}{0pt}
   \flx@marginparcorrection
}

\def\flx@LS@rot{%
  \setbox\@outputbox\vbox{\hbox{\rotatebox{90}{\box\@outputbox}}}}

\def\flx@landscape{%
  \let\flx@LS@makecol=\@makecol
  \let\flx@LS@makefcolumn=\@makefcolumn
  \clearpage
  \begingroup
  \global\vsize=\textwidth
  \global\hsize=\textheight
  \linewidth=\hsize
  \columnwidth=\hsize
  \@colroom=\vsize
  \textheight=\vsize
  \@colht=\vsize
  \def\@makecol{\flx@LS@makecol\flx@LS@rot}%
  \def\@makefcolumn##1{\flx@LS@makefcolumn{##1}\flx@LS@rot}}

\def\flx@endlandscape{%
  \clearpage
  \endgroup
  \global\@colht=\textheight
  \global\vsize=\textheight
  \global\@colroom=\textheight}
\NewDocumentCommand\flx@marginparcorrection{}{
\ifFix@Margin
   \setlength{\marginparsepodd}{\marginparsep-\flx@orgtextwidth+\textwidth}
   \setlength{\marginparsepeven}{\marginparsep}
   \patchcmd{\@addmarginpar}%In this command
   {\mph@orig@addmarginpar}%...replace this...
   {\if@twoside\ifodd\c@page\relax%...with this
         \marginparsep=\marginparsepodd%Page isodd
      \else%twoside and eveb
         \marginparsep=\marginparsepeven%Page iseven
      \fi
      \else% oneside
         \marginparsep=\marginparsepodd
      \fi
   \mph@orig@addmarginpar}
   {}%success
   {\message{Error! Couldn't hook into command   %failure
   `\string\@addmarginpar'}} %end patch command

   {
   \def\patcherr{%
      \message{Error!Couldn't hook into command`\string\@mn@@@marginnote'}}
      \def\patchok{%
      \let\patcherr\relax%Only display error if first patch fails
      \patch%Now patch again.
   }
   \def\patch{
      \patchcmd{\@mn@@@marginnote}%In this command
         {\kern\marginparsep}%...replace this...
      {\ifx\@mn@currpage\relax\else%...with this
         \if@twoside\ifodd\@mn@currpage\relax
            \kern\marginparsepodd%Page is odd
         \else
            \kern\marginparsepeven
         \fi
         \else% one side
            \kern\marginparsepodd
         \fi
      \fi}
      {\message{Patched!}\patchok}%success(recurse)
      {\patcherr}%fail
   }
   \message{Patching`\string\@mn@@@marginnote`!}
   \patch
   \global\let\@mn@@@marginnote\@mn@@@marginnote%Makepatch global
   }
\fi
}
\ifSF@frame\RequirePackage{eso-pic}\fi  % so that below code can draw frames

\ifSF@frame\AddToShipoutPictureBG{\flx@ShowFramePicture}\fi

\newcommand*\flx@framecolor{\color{green}}
\newcommand*\flx@frameline{.8pt}
\newdimen\SF@tempdima

\newcommand\flx@ShowFramePicture{%
  \begingroup
    \color{orange}\linethickness{\flx@frameline}%
    \if@twoside
       \ifodd\c@page
          \AtPageUpperLeft{%
            \put(\flx@innerbleed,-\paperheight+\flx@outerbleed){%
              \framebox(\paperwidth-\flx@outerbleed-\flx@innerbleed,\paperheight-2\flx@outerbleed){}}}%
        \else
          \AtPageUpperLeft{%
            \put(\flx@outerbleed,-\paperheight+\flx@outerbleed){%
              \framebox(\paperwidth-\flx@outerbleed-\flx@innerbleed,\paperheight-2\flx@outerbleed){}}}%
        \fi
    \else
       \AtPageUpperLeft{%
         \put(\flx@innerbleed,-\paperheight+\flx@outerbleed){%
           \framebox(\paperwidth-\flx@outerbleed-\flx@innerbleed,\paperheight-2\flx@outerbleed){}}}%
    \fi
    \flx@framecolor\linethickness{\flx@frameline}%
    \AtTextLowerLeft{%
      \put(0,-\flx@LSfix){\framebox(\textwidth,\textheight+\flx@LSfix){}}}%

\ifUse@fancyhdr
    \if@twoside
        \ifodd\c@page
            \AtPageUpperLeft{%
               \put(\flx@inner+\flx@binding,-\flx@outerbleed-\flx@top-\headheight){%
                    \framebox(\flx@linewidth,\headheight){}}}%
            \AtPageUpperLeft{%
               \put(\flx@inner+\flx@binding,%
                       -\flx@outerbleed-\flx@top-\headheight-\headsep-\textheight%
                       -\footskip -\flx@LSfix){%
                    \line(1,0){\flx@linewidth}}}%
        \else
        \AtPageUpperLeft{%
           \put(\flx@outer+\flx@outerbleed,-\flx@outerbleed-\flx@top-\headheight){%
            \framebox(\flx@linewidth,\headheight){}}}%
        \AtPageUpperLeft{%
           \put(\flx@outer+\flx@outerbleed,%
                   -\flx@outerbleed-\flx@top-\headheight-\headsep-\textheight%
                   -\footskip -\flx@LSfix){%
                \line(1,0){\flx@linewidth}}}%

        \fi
    \else
        \AtPageUpperLeft{%
           \put(\flx@inner+\flx@binding,-\flx@outerbleed-\flx@top-\headheight){%
            \framebox(\flx@linewidth,\headheight){}}}%
        \AtPageUpperLeft{%
           \put(\flx@inner+\flx@binding,%
                   -\flx@outerbleed-\flx@top-\headheight-\headsep-\textheight%
                   -\footskip -\flx@LSfix){%
                \line(1,0){\flx@linewidth}}}%
    \fi
\else
    \AtTextUpperLeft{%
      \put(0,\headsep){%
        \framebox(\textwidth,\headheight){}}}%
    \AtTextLowerLeft{%
      \put(0,-\footskip-\flx@LSfix){%
        \line(1,0){\textwidth}}}%
\fi
\FPifgt\flx@m 0{%
    \AtTextLowerLeft{%
      \SF@tempdima=\textwidth\advance\SF@tempdima\marginparsep%
      \if@twoside
        \ifodd\c@page\else
          \if@mparswitch
            \SF@tempdima=-\marginparsep\advance\SF@tempdima-\marginparwidth
          \fi
        \fi
      \fi
      \put(\SF@tempdima,0)%
        {\framebox(\marginparwidth,\textheight){}}%
    }%
  }\fi
  \endgroup
}
\endinput
%%
%% End of file `flexipage.sty'.
