| 1 |
%%%============================================================================== |
| 2 |
% WinEdt pragmas |
| 3 |
% !Mode:: "TeX:EN" |
| 4 |
% Default Compile engines: |
| 5 |
% !TEX program = pdflatex |
| 6 |
% !PDFTeXify ext = --enable-etex --restrict-write18 |
| 7 |
% !PDFLaTeX ext = --enable-etex --restrict-write18 |
| 8 |
% !BIB program = biber |
| 9 |
%%%============================================================================== |
| 10 |
%% Copyright 2026-present by Alceu Frigeri |
| 11 |
%% |
| 12 |
%% This work may be distributed and/or modified under the conditions of |
| 13 |
%% |
| 14 |
%% * The [LaTeX Project Public License](http://www.latex-project.org/lppl.txt), |
| 15 |
%% version 1.3c (or later), and/or |
| 16 |
%% * The [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html), |
| 17 |
%% version 3 (or later) |
| 18 |
%% |
| 19 |
%% This work has the LPPL maintenance status *maintained*. |
| 20 |
%% |
| 21 |
%% The Current Maintainer of this work is Alceu Frigeri |
| 22 |
%% |
| 23 |
%% This is version {1.0} {2026/02/18} |
| 24 |
%% |
| 25 |
%% The list of files that compose this work can be found in the README.md file at |
| 26 |
%% https://ctan.org/pkg/tokglobalstack |
| 27 |
%% |
| 28 |
%%%============================================================================== |
| 29 |
\documentclass[10pt]{article} |
| 30 |
\RequirePackage[verbose,a4paper,marginparwidth=27.5mm,top=2.5cm,bottom=1.5cm,hmargin={40mm,20mm},marginparsep=2.5mm,columnsep=10mm,asymmetric]{geometry} |
| 31 |
\usepackage[T1]{fontenc} |
| 32 |
\usepackage[utf8]{inputenc} |
| 33 |
\usepackage{lmodern} |
| 34 |
\usepackage[infograb,silence]{codedescribe} |
| 35 |
\RequirePackage[inline]{enumitem} |
| 36 |
\SetEnumitemKey{miditemsep}{parsep=0ex,itemsep=0.4ex} |
| 37 |
|
| 38 |
\usepackage{tokglobalstack} |
| 39 |
|
| 40 |
%% if needed |
| 41 |
\RequirePackage[backend=biber]{biblatex} |
| 42 |
\addbibresource{tokglobalstack.bib} |
| 43 |
|
| 44 |
\newcodekey{globalstack} |
| 45 |
{ |
| 46 |
letter = {_}, |
| 47 |
texcs = [3]{globalstack_csnew, globalstack_new, globalstack_gpush, globalstack_gput_right, globalstack_gput_left, globalstack_gpop }, |
| 48 |
emph = [3]{myStackA_gpush,myStackA_gput_left,myStackA_gput_right,myStackA_gpop,g_myStackB_stack}, |
| 49 |
keywd = [2]{l__mytest_tmpa_bool,l__mytest_tmpb_bool,l__mytest_tmpc_bool,mytest_show_bools}, |
| 50 |
codeprefix = {}, |
| 51 |
resultprefix = {}, |
| 52 |
} |
| 53 |
|
| 54 |
\RequirePackage[hidelinks,hypertexnames=false]{hyperref} |
| 55 |
|
| 56 |
\begin{document} |
| 57 |
\tstitle{ |
| 58 |
author={Alceu Frigeri\footnote{\tsfmt{\url{https://github.com/alceu-frigeri/tokglobalstack}}}}, |
| 59 |
date={\tsdate}, |
| 60 |
title={The tokglobalstack Package\break Version \PkgInfo{tokglobalstack}{version}} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
\begin{typesetabstract} |
| 65 |
|
| 66 |
This package offers two stack's implementations. Those stacks can be used to, for instance, preserve some tokens beyond a variable number of nested groups, or to implement recursive commands without relying on local groups. |
| 67 |
\end{typesetabstract} |
| 68 |
|
| 69 |
\tableofcontents |
| 70 |
|
| 71 |
\tsOn |
| 72 |
|
| 73 |
\section{Introduction} |
| 74 |
When it's necessary to preserve the value of some tokens beyond a local group, it's enough, in simple cases, to use !:[code,or]{\group_insert_after:N,\aftergroup}. |
| 75 |
|
| 76 |
But, sometimes you don't have (or can't use) this information, see \cite{exchange}, for instance. For those cases, this package deploys a strategy (presented and compared in \cite{xstacks}) based on stacks. |
| 77 |
|
| 78 |
These stacks can also be used to write recursive commands (preserving variables) without having to rely on local group scoping. |
| 79 |
|
| 80 |
|
| 81 |
\section{Stack Variable} |
| 82 |
|
| 83 |
\begin{codedescribe}{\globalstack_new:N,\globalstack_gpush:Nn,\globalstack_gput_right:Nn,\globalstack_gput_left:Nn,\globalstack_gpop:N} |
| 84 |
\begin{codesyntax}% |
| 85 |
!!?{\globalstack_new:N}{stack-var} |
| 86 |
!!?{\globalstack_gpush:Nn}{stack-var,tokens} |
| 87 |
!!?{\globalstack_gput_right:Nn}{stack-var,tokens} |
| 88 |
!!?{\globalstack_gput_left:Nn}{stack-var,tokens} |
| 89 |
!!?{\globalstack_gpop:N}{stack-var} |
| 90 |
\end{codesyntax} |
| 91 |
!:{\globalstack_new:N} will globally create a stack variable named !:[marg]{stack-var} (a specialized token list variable). Once created it is possible to push tokens into it (!:{\globalstack_gpush:Nn}), amend tokens to the top (!:{\globalstack_gput_right:Nn,\globalstack_gput_left:Nn}) and pop those tokens (!:{\globalstack_gpop:N}) into the input stream. All assignments being global. |
| 92 |
\end{codedescribe} |
| 93 |
\begin{tsremark} |
| 94 |
An error will be raised if !:[marg]{stack-var} is already defined. |
| 95 |
\end{tsremark} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
\section{Custom Stack Commands} |
| 100 |
|
| 101 |
\begin{codedescribe}{\globalstack_csnew:n} |
| 102 |
\begin{codesyntax}% |
| 103 |
!!?{\globalstack_csnew:n}{stack-prefix} |
| 104 |
\end{codesyntax} |
| 105 |
This will globally create a set of commands, named after !:[marg]{stack-prefix}, to push, put and pop items from a private global stack. All assignments to/from that stack will be global, and the stack itself will be unique to the command's set. |
| 106 |
\end{codedescribe} |
| 107 |
\begin{tsremark} |
| 108 |
An error will be raised if !:[marg]{stack-prefix} is already used. |
| 109 |
\end{tsremark} |
| 110 |
|
| 111 |
\begin{codedescribe}{\<stack-prefix>_gpush:n,\<stack-prefix>_gput_right:n,\<stack-prefix>_gput_left:n,\<stack-prefix>_gpop:} |
| 112 |
\begin{codesyntax}% |
| 113 |
!!?{\<stack-prefix>_gpush:n}{tokens} |
| 114 |
!!?{\<stack-prefix>_gput_right:n}{tokens} |
| 115 |
!!?{\<stack-prefix>_gput_left:n}{tokens} |
| 116 |
!!?{\<stack-prefix>_gput_gpop:}{} |
| 117 |
\end{codesyntax} |
| 118 |
The !:{\<stack-prefix>_gpush:n} will push !:[marg]{tokens} (can be any number of tokens) into a global, private, stack. !:{\<stack-prefix>_gput_right:n,\<stack-prefix>_gput_left:n} will amend tokens to it, and !:{\<stack-prefix>_gpop:}, as the name implies, will insert the top of the stack into the input stream. That way it is possible to have a very fine control of what, where and when the items are collected and used. |
| 119 |
\end{codedescribe} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
\section{Examples of Use} |
| 124 |
In the following examples, two stacks will be used, !:[marg]{myStackA,myStackB} (one of each kind). |
| 125 |
|
| 126 |
\begin{codestore}[globalstack] |
| 127 |
\ExplSyntaxOn |
| 128 |
% Just a set of booleans for testing |
| 129 |
\bool_new:N \l__mytest_tmpa_bool |
| 130 |
\bool_new:N \l__mytest_tmpb_bool |
| 131 |
\bool_new:N \l__mytest_tmpc_bool |
| 132 |
\cs_new:Npn \mytest_show_bools:n #1 |
| 133 |
{ \underline{#1:}\par |
| 134 |
\bool_if:NTF \l__mytest_tmpa_bool {{\color{red}a~true}}{a~false} ~-~ |
| 135 |
\bool_if:NTF \l__mytest_tmpb_bool {{\color{red}b~true}}{b~false} ~-~ |
| 136 |
\bool_if:NTF \l__mytest_tmpc_bool {{\color{red}c~true}}{c~false} \par |
| 137 |
} |
| 138 |
|
| 139 |
\globalstack_csnew:n {myStackA} |
| 140 |
\globalstack_new:N \g_myStackB_stack |
| 141 |
\ExplSyntaxOff |
| 142 |
\end{codestore} |
| 143 |
|
| 144 |
\tscode*[globalstack]{globalstack} |
| 145 |
\tsexec{globalstack} |
| 146 |
|
| 147 |
|
| 148 |
\subsection{Using a Stack Variable} |
| 149 |
Using just one position (of the stack) and restoring all tokens in a single point. |
| 150 |
\begin{codestore}[globalstack] |
| 151 |
\ExplSyntaxOn |
| 152 |
\group_begin: |
| 153 |
{{ \globalstack_gpush:Nn \g_myStackB_stack |
| 154 |
{\bool_set_true:N \l__mytest_tmpa_bool} |
| 155 |
{{ \globalstack_gput_right:Nn \g_myStackB_stack |
| 156 |
{\bool_set_true:N \l__mytest_tmpb_bool} |
| 157 |
\mytest_show_bools:n {T1} |
| 158 |
}} \mytest_show_bools:n {T2} |
| 159 |
}} \globalstack_gpop:N \g_myStackB_stack |
| 160 |
\mytest_show_bools:n {T3} |
| 161 |
\group_end: |
| 162 |
\ExplSyntaxOff |
| 163 |
\end{codestore} |
| 164 |
|
| 165 |
|
| 166 |
\tsdemo[globalstack]{globalstack}[2] |
| 167 |
|
| 168 |
\newpage |
| 169 |
Using two positions (of the stack) and restoring the tokens in separated points. |
| 170 |
|
| 171 |
\begin{codestore}[globalstack] |
| 172 |
\ExplSyntaxOn |
| 173 |
\group_begin: |
| 174 |
{{ \globalstack_gpush:Nn \g_myStackB_stack |
| 175 |
{\bool_set_true:N \l__mytest_tmpa_bool} |
| 176 |
{{ \globalstack_gpush:Nn \g_myStackB_stack |
| 177 |
{\bool_set_true:N \l__mytest_tmpb_bool} |
| 178 |
\mytest_show_bools:n {T1} |
| 179 |
}} \globalstack_gpop:N \g_myStackB_stack |
| 180 |
\mytest_show_bools:n {T2} |
| 181 |
}} \globalstack_gpop:N \g_myStackB_stack |
| 182 |
\mytest_show_bools:n {T3} |
| 183 |
\group_end: |
| 184 |
\ExplSyntaxOff |
| 185 |
\end{codestore} |
| 186 |
|
| 187 |
\tsdemo[globalstack]{globalstack}[3] |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
\subsection{Using Custom Stack Commands} |
| 192 |
Using just one position (of the stack) and restoring all tokens in a single point. |
| 193 |
\begin{codestore}[globalstack] |
| 194 |
\ExplSyntaxOn |
| 195 |
\group_begin: |
| 196 |
{{ \myStackA_gpush:n |
| 197 |
{\bool_set_true:N \l__mytest_tmpa_bool} |
| 198 |
{{ \myStackA_gput_right:n |
| 199 |
{\bool_set_true:N \l__mytest_tmpb_bool} |
| 200 |
\mytest_show_bools:n {T1} |
| 201 |
}} \mytest_show_bools:n {T2} |
| 202 |
}} \myStackA_gpop: |
| 203 |
\mytest_show_bools:n {T3} |
| 204 |
\group_end: |
| 205 |
\ExplSyntaxOff |
| 206 |
\end{codestore} |
| 207 |
|
| 208 |
\tsdemo[globalstack]{globalstack}[4] |
| 209 |
|
| 210 |
Using two positions (of the stack) and restoring the tokens in separated points. |
| 211 |
\begin{codestore}[globalstack] |
| 212 |
\ExplSyntaxOn |
| 213 |
\group_begin: |
| 214 |
{{ \myStackA_gpush:n |
| 215 |
{\bool_set_true:N \l__mytest_tmpa_bool} |
| 216 |
{{ \myStackA_gpush:n |
| 217 |
{\bool_set_true:N \l__mytest_tmpb_bool} |
| 218 |
\mytest_show_bools:n {T1} |
| 219 |
}} \myStackA_gpop: |
| 220 |
\mytest_show_bools:n {T2} |
| 221 |
}} \myStackA_gpop: |
| 222 |
\mytest_show_bools:n {T3} |
| 223 |
\group_end: |
| 224 |
\ExplSyntaxOff |
| 225 |
\end{codestore} |
| 226 |
|
| 227 |
\tsdemo[globalstack]{globalstack}[5] |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
%\ExplSyntaxOn |
| 232 |
%\cs_show:N |
| 233 |
%\ExplSyntaxOff |
| 234 |
|
| 235 |
\tsOff |
| 236 |
|
| 237 |
|
| 238 |
%% if needed |
| 239 |
\printbibliography |
| 240 |
|
| 241 |
\end{document} |