%%
%% This is file `marginalia.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% marginalia.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2025 Alan J. Cain
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.3c 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.
%% 
\NeedsTeXFormat{LaTeX2e}[2020-02-02]
\ProvidesExplPackage{marginalia}{2025-02-18}{0.80.2}
  {Non-floating marginal content for LuaLaTeX}
\sys_if_engine_luatex:F
  {
    \msg_new:nnn{marginalia}{lualatex_required}
      {LuaLaTeX~required.~Package~loading~will~abort.}
    \msg_critical:nn{marginalia}{lualatex_required}
  }
\int_new:N\l__marginalia_type_int
\keys_define:nn { marginalia }
{
  type .choices:nn = {normal,fixed,optfixed}{
    \int_set:Nn\l__marginalia_type_int{\l_keys_choice_int}
  },
  type .initial:n = normal,
}
\int_new:N\l__marginalia_pos_int
\keys_define:nn { marginalia }
{
  pos .choices:nn = {auto,reverse,left,right,nearest}{
    \int_set:Nn\l__marginalia_pos_int{\l_keys_choice_int}
  },
  pos .initial:n = auto
}
\int_new:N\l__marginalia_column_int
\keys_define:nn { marginalia }
{
  column .choices:nn = {auto,one,left,right}{
    \int_set:Nn\l__marginalia_column_int{\l_keys_choice_int-2}
  },
  column .initial:n = auto,
}
\keys_define:nn { marginalia }
{
  xsep~recto~outer .dim_set:N = \l__marginalia_xsep_recto_outer_dim,
  xsep~recto~inner .dim_set:N = \l__marginalia_xsep_recto_inner_dim,
  xsep~verso~outer .dim_set:N = \l__marginalia_xsep_verso_outer_dim,
  xsep~verso~inner .dim_set:N = \l__marginalia_xsep_verso_inner_dim,
  xsep~right~between .dim_set:N = \l__marginalia_xsep_right_between_dim,
  xsep~left~between .dim_set:N = \l__marginalia_xsep_left_between_dim,
  xsep .code:n = {
    \keys_set:nn{ marginalia }{
      xsep~recto~outer=#1,
      xsep~recto~inner=#1,
      xsep~verso~outer=#1,
      xsep~verso~inner=#1,
      xsep~right~between=#1,
      xsep~left~between=#1,
    }
  },
  xsep~outer .code:n = {
    \keys_set:nn{ marginalia }{
      xsep~recto~outer=#1,
      xsep~verso~outer=#1,
    }
  },
  xsep~inner .code:n = {
    \keys_set:nn{ marginalia }{
      xsep~recto~inner=#1,
      xsep~verso~inner=#1,
    }
  },
  xsep~between .code:n = {
    \keys_set:nn{ marginalia }{
      xsep~right~between=#1,
      xsep~left~between=#1,
    }
  },
  xsep .initial:n = \marginparsep,
}
\int_new:N\l__marginalia_valign_int
\keys_define:nn { marginalia }
{
  valign .choices:nn = {t,b}{
    \int_set_eq:NN\l__marginalia_valign_int\l_keys_choice_int
  },
  valign .initial:n = t,
}
\keys_define:nn { marginalia }
{
  yshift .dim_set:N = \l__marginalia_default_yshift_dim,
  yshift .initial:n = 0pt,
}
\keys_define:nn { marginalia }
{
  ysep~above .dim_set:N = \l__marginalia_ysep_above_dim,
  ysep~below .dim_set:N = \l__marginalia_ysep_below_dim,
  ysep~page~top .dim_set:N = \l__marginalia_ysep_page_top_dim,
  ysep~page~bottom .dim_set:N = \l__marginalia_ysep_page_bottom_dim,
  ysep .code:n = {
    \keys_set:nn{ marginalia }{
      ysep~below=#1,
      ysep~above=#1,
      ysep~page~top=#1,
      ysep~page~bottom=#1,
    }
  },
  ysep .initial:n = \marginparpush,
}
\keys_define:nn { marginalia }
{
  width~recto~outer .dim_set:N = \l__marginalia_width_recto_outer_dim,
  width~recto~inner .dim_set:N = \l__marginalia_width_recto_inner_dim,
  width~verso~outer .dim_set:N = \l__marginalia_width_verso_outer_dim,
  width~verso~inner .dim_set:N = \l__marginalia_width_verso_inner_dim,
  width~right~between .dim_set:N = \l__marginalia_width_right_between_dim,
  width~left~between .dim_set:N = \l__marginalia_width_left_between_dim,
  width .code:n = {
    \keys_set:nn{ marginalia }{
      width~recto~outer=#1,
      width~recto~inner=#1,
      width~verso~outer=#1,
      width~verso~inner=#1,
      width~right~between=#1,
      width~left~between=#1,
    }
  },
  width~outer .code:n = {
    \keys_set:nn{ marginalia }{
      width~recto~outer=#1,
      width~verso~outer=#1,
    }
  },
  width~inner .code:n = {
    \keys_set:nn{ marginalia }{
      width~recto~inner=#1,
      width~verso~inner=#1,
    }
  },
  width~between .code:n = {
    \keys_set:nn{ marginalia }{
      width~right~between=#1,
      width~left~between=#1,
    }
  },
  width .initial:n = \marginparwidth,
}
%% \begin{macro}{
\keys_define:nn { marginalia }
{
  style~recto~outer .tl_set:N = \l__marginalia_style_recto_outer_tl,
  style~recto~inner .tl_set:N = \l__marginalia_style_recto_inner_tl,
  style~verso~outer .tl_set:N = \l__marginalia_style_verso_outer_tl,
  style~verso~inner .tl_set:N = \l__marginalia_style_verso_inner_tl,
  style~right~between .tl_set:N = \l__marginalia_style_right_between_tl,
  style~left~between .tl_set:N = \l__marginalia_style_left_between_tl,
  style .code:n = {
    \keys_set:nn{ marginalia }{
      style~recto~outer=#1,
      style~recto~inner=#1,
      style~verso~outer=#1,
      style~verso~inner=#1,
      style~right~between=#1,
      style~left~between=#1,
    }
  },
  style .initial:n = {},
}
  \lua_now:n{
    marginalia = require('marginalia')
  }
\cs_new:Npn\__marginalia_lua_store_default_page_data:
  {
    \lua_now:n{ marginalia.store_default_page_data() }
  }
\cs_new:Npn\__marginalia_lua_store_page_data:n #1
  {
    \lua_now:n{ marginalia.store_page_data('#1') }
  }
\cs_new:Npn\__marginalia_lua_check_page_data:n #1
  {
    \lua_now:n{ marginalia.check_page_data('#1') }
  }
\cs_new:Npn\__marginalia_lua_write_page_change_report:
  {
    \lua_now:n{ marginalia.write_page_change_report() }
  }
\cs_new:Npn\__marginalia_lua_store_item_data:n #1
  {
    \lua_now:n{ marginalia.store_item_data('#1') }
  }
\cs_new:Npn\__marginalia_lua_check_item_data:n #1
  {
    \lua_now:n{ marginalia.check_item_data('#1') }
  }
\cs_new:Npn\__marginalia_lua_compute_items:
  {
    \lua_now:n{ marginalia.compute_items() }
  }
\cs_new:Npn\__marginalia_lua_write_problem_report:
  {
    \lua_now:n{ marginalia.write_problem_report() }
  }
\cs_new:Npn\__marginalia_lua_write_item_change_report:
  {
    \lua_now:n{ marginalia.write_item_change_report() }
  }
\cs_new:Npn\__marginalia_lua_load_item_data:n #1
  {
    \lua_now:e{ marginalia.load_item_data('#1') }
  }
\NewDocumentCommand{\marginalia@pagedata}{ m }{
  \__marginalia_process_page_data:n{#1}
}
\cs_set_eq:NN
  \__marginalia_process_page_data:n
  \__marginalia_lua_store_page_data:n
\DeclareDocumentCommand{\marginalia@itemdata}{ m }{
  \__marginalia_process_item_data:n{#1}
}
\cs_set_eq:NN
  \__marginalia_process_item_data:n
  \__marginalia_lua_store_item_data:n
\AddToHook{begindocument}{
  \__marginalia_lua_store_default_page_data:
  \__marginalia_lua_compute_items:
  \cs_set_eq:NN\l__marginalia_aux_iow\@mainaux
}
\AddToHook{enddocument/afterlastpage}{
  \cs_set_eq:NN
    \__marginalia_process_page_data:n
    \__marginalia_lua_check_page_data:n
  \cs_set_eq:NN
    \__marginalia_process_item_data:n
    \__marginalia_lua_check_item_data:n
  }
\msg_new:nnn{marginalia}{placement_problem}
  { Problems~in~placement.~#1 }
\msg_new:nnn{marginalia}{item_change}
  { Changes~in~item~data.~#1 }
\msg_new:nnn{marginalia}{page_change}
  { Changes~in~page~data.~#1 }
\cs_new:Npn\__marginalia_write_reports:
  {
    \group_begin:
    \tl_set:Ne\l_tmpa_tl{\__marginalia_lua_write_problem_report:}
    \tl_if_blank:VF\l_tmpa_tl
      {
        \msg_warning:nne{marginalia}{placement_problem}{\tl_use:N\l_tmpa_tl}
      }
    \tl_set:Ne\l_tmpa_tl{\__marginalia_lua_write_item_change_report:}
    \tl_if_blank:VF\l_tmpa_tl
      {
        \msg_warning:nne{marginalia}{item_change}{\tl_use:N\l_tmpa_tl}
      }
    \tl_set:Ne\l_tmpa_tl{\__marginalia_lua_write_page_change_report:}
    \tl_if_blank:VF\l_tmpa_tl
      {
        \msg_warning:nne{marginalia}{page_change}{\tl_use:N\l_tmpa_tl}
      }
    \group_end:
  }
\AddToHook{enddocument/info}{
  \__marginalia_write_reports:
}
\int_new:N\g__marginalia_pagedatano_int
\cs_set_eq:NN\__marginalia_write_page_data:\prg_do_nothing:
\cs_new:Npn\__marginalia_write_page_data_real:
  {
    \int_gincr:N\g__marginalia_pagedatano_int
    \iow_now:Ne\l__marginalia_aux_iow{
      \token_to_str:N\marginalia@pagedata{
        pagedatano=\int_value:w\g__marginalia_pagedatano_int,
        abspageno=\int_eval:n{\g_shipout_readonly_int+1},
        hoffset=\int_value:w\hoffset,
        voffset=\int_value:w\voffset,
        paperheight=\int_value:w\paperheight,
        oddsidemargin=\int_value:w\oddsidemargin,
        evensidemargin=\int_value:w\evensidemargin,
        textwidth=\int_value:w\textwidth,
        columncount=\int_value:w\col@number,
        columnwidth=\int_value:w\columnwidth,
        columnsep=\int_value:w\columnsep,
        twoside=\bool_to_str:n{\legacy_if_p:n{@twoside}},
      }
    }
  }
\AddToHook{begindocument/end}
  {
    \cs_set_eq:NN
      \__marginalia_write_page_data:
      \__marginalia_write_page_data_real:
    \__marginalia_write_page_data:
  }
\int_new:N\g__marginalia_itemno_int
\box_new:N\l__marginalia_item_box
\dim_new:N\l__marginalia_item_height_dim
\dim_new:N\l__marginalia_item_depth_dim
\int_new:N\l__marginalia_page_int
\int_new:N\l__marginalia_column_computed_int
\dim_new:N\l__marginalia_xshift_computed_dim
\dim_new:N\l__marginalia_yshift_computed_dim
\int_new:N\l__marginalia_side_computed_int
\int_new:N\l__marginalia_marginno_computed_int
\int_new:N\l__marginalia_enabled_computed_int
\cs_new:Npn\__marginalia_process_item:nn #1#2
  {
    \int_gincr:N\g__marginalia_itemno_int
    \group_begin:
      \keys_set:nn{marginalia}{ #1 }
      \__marginalia_lua_load_item_data:n
        { \int_value:w\g__marginalia_itemno_int }
      \mode_if_math:TF
        {
          \cs_set_eq:NN
            \__marginalia_typeset:n
            \__marginalia_typeset_mmode:n
        }
        {
          \legacy_if:nT{@inlabel}
            { \leavevmode }
          \mode_if_horizontal:TF
            {
              \cs_set_eq:NN
                \__marginalia_typeset:n
                \__marginalia_typeset_hmode:n
            }
            {
              \cs_set_eq:NN
                \__marginalia_typeset:n
                \__marginalia_typeset_vmode:n
            }
        }
      \int_compare:nNnTF{\l__marginalia_valign_int}={2}
        {
          \cs_set_eq:NN\__marginalia_item_box_set:Nn\vbox_set:Nn
        }
        {
          \cs_set_eq:NN\__marginalia_item_box_set:Nn\vbox_set_top:Nn
        }
      \__marginalia_set_xsep_width_style:
      \__marginalia_item_box_set:Nn\l__marginalia_item_box{
        \@parboxrestore
        \normalfont\normalsize

        \tl_use:N\l__marginalia_style_tl
        \dim_set_eq:NN\hsize\l__marginalia_width_dim
        \dim_set_eq:NN\linewidth\hsize

        \cs_set_eq:NN\marginaliapage\l__marginalia_page_int
        \cs_set_eq:NN\marginaliacolumn\l__marginalia_column_computed_int

        \group_begin:
        \ignorespaces
        #2
        \par
        \group_end:
      }
      \dim_set:Nn\l__marginalia_item_height_dim
        {\box_ht:N\l__marginalia_item_box}
      \dim_set:Nn\l__marginalia_item_depth_dim
        {\box_dp:N\l__marginalia_item_box}
      \__marginalia_typeset:n{
        \savepos
        \iow_shipout_e:Ne\l__marginalia_aux_iow{
          \token_to_str:N\marginalia@itemdata{
            itemno=\int_value:w\g__marginalia_itemno_int,
            abspageno=\exp_not:N\int_eval:n{\g_shipout_readonly_int},
            pageno=\exp_not:N\int_value:w\c@page,
            type=\str_use:N\int_value:w\l__marginalia_type_int,
            xpos=\exp_not:N\int_value:w\lastxpos,
            ypos=\exp_not:N\int_value:w\lastypos,
            height=\int_value:w\l__marginalia_item_height_dim,
            depth=\int_value:w\l__marginalia_item_depth_dim,
            pos=\int_value:w\l__marginalia_pos_int,
            column=\int_value:w\l__marginalia_column_int,
            yshift=\int_value:w\l__marginalia_default_yshift_dim,
            ysep~above=\int_value:w\l__marginalia_ysep_above_dim,
            ysep~below=\int_value:w\l__marginalia_ysep_below_dim,
            ysep~page~top=\int_value:w\l__marginalia_ysep_page_top_dim,
            ysep~page~bottom=\int_value:w\l__marginalia_ysep_page_bottom_dim,
          }
        }
      \int_if_zero:nF{\l__marginalia_enabled_computed_int}
        {
          \int_if_zero:nTF{\l__marginalia_side_computed_int}
            {
              \rlap{
                \kern\l__marginalia_xshift_computed_dim
                \kern\l__marginalia_xsep_dim
                \__marginalia_place_item_box:
              }
            }
            {
              \llap{
                \__marginalia_place_item_box:
                \kern\l__marginalia_xsep_dim
                \kern-\l__marginalia_xshift_computed_dim
              }
            }
        }
      }
    \group_end:
  }
\cs_new:Npn\__marginalia_set_xsep_width_style:
  {
    \int_case:nn{\l__marginalia_marginno_computed_int}
      {
        {0}
        {
          \cs_set_eq:NN\l__marginalia_xsep_dim
            \l__marginalia_xsep_recto_outer_dim
          \cs_set_eq:NN\l__marginalia_width_dim
            \l__marginalia_width_recto_outer_dim
          \cs_set_eq:NN\l__marginalia_style_tl
            \l__marginalia_style_recto_outer_tl
        }
        {1}
        {
          \cs_set_eq:NN\l__marginalia_xsep_dim
            \l__marginalia_xsep_recto_inner_dim
          \cs_set_eq:NN\l__marginalia_width_dim
            \l__marginalia_width_recto_inner_dim
          \cs_set_eq:NN\l__marginalia_style_tl
            \l__marginalia_style_recto_inner_tl
        }
        {2}
        {
          \cs_set_eq:NN\l__marginalia_xsep_dim
            \l__marginalia_xsep_verso_outer_dim
          \cs_set_eq:NN\l__marginalia_width_dim
            \l__marginalia_width_verso_outer_dim
          \cs_set_eq:NN\l__marginalia_style_tl
            \l__marginalia_style_verso_outer_tl
        }
        {3}
        {
          \cs_set_eq:NN\l__marginalia_xsep_dim
            \l__marginalia_xsep_verso_inner_dim
          \cs_set_eq:NN\l__marginalia_width_dim
            \l__marginalia_width_verso_inner_dim
          \cs_set_eq:NN\l__marginalia_style_tl
            \l__marginalia_style_verso_inner_tl
        }
        {4}
        {
          \cs_set_eq:NN\l__marginalia_xsep_dim
            \l__marginalia_xsep_right_between_dim
          \cs_set_eq:NN\l__marginalia_width_dim
            \l__marginalia_width_right_between_dim
          \cs_set_eq:NN\l__marginalia_style_tl
            \l__marginalia_style_right_between_tl
        }
        {5}
        {
          \cs_set_eq:NN\l__marginalia_xsep_dim
            \l__marginalia_xsep_left_between_dim
          \cs_set_eq:NN\l__marginalia_width_dim
            \l__marginalia_width_left_between_dim
          \cs_set_eq:NN\l__marginalia_style_tl
            \l__marginalia_style_left_between_tl
        }
      }
  }
\cs_new:Npn\__marginalia_place_item_box:
  {
    \smash
      {
        \box_move_up:nn{\l__marginalia_yshift_computed_dim}
          {
            \box_use:N\l__marginalia_item_box
          }
      }
  }
\cs_new:Npn\__marginalia_typeset_mmode:n #1
  {
    #1
  }
\cs_new:Npn\__marginalia_typeset_hmode:n #1
  {
    \@bsphack
    #1
    \@esphack
  }
\cs_new:Npn\__marginalia_typeset_vmode:n #1
  {
    \nobreak\noindent\strut #1\par
    \skip_vertical:n{-\baselineskip}
  }
\NewDocumentCommand{\marginalia}{ O{} +m }
  {
    \__marginalia_process_item:nn{#1}{#2}
  }
\NewDocumentCommand{\marginaliasetup}{ m }
{
  \keys_set:nn{marginalia}{ #1 }
}
\NewDocumentCommand{\marginalianewgeometry}{}
{
  \__marginalia_write_page_data:
}
