Start creating hierarchical label/reference system.

This commit is contained in:
2025-09-16 16:20:39 -05:00
parent f4ba849bb3
commit 8cd1a056d6
4 changed files with 262 additions and 84 deletions

158
packages/CrossRef.sty Normal file
View File

@@ -0,0 +1,158 @@
\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{PrimeTeX/packages/CrossRef}{2025-09-16}{1.0}{
Custom label and reference commands.
}
\prop_const_from_keyval:Nn \c_section_names_prop {
0 = Part\nobreakspace,
1 = Chapter\nobreakspace,
2 = Section\nobreakspace,
3 = Subsection\nobreakspace,
4 = Sub-subsection\nobreakspace,
5 = Paragraph\nobreakspace,
6 = Subparagraph\nobreakspace,
9 = Appendix\nobreakspace,
}
\prop_const_from_keyval:Nn \c_section_levels_prop {
part = 0,
chapter = 1,
section = 2,
subsection = 3,
subsubsection = 4,
paragraph = 5,
subparagraph = 6,
}
\str_const:Nn \c_label_delimiter_str {/}
\str_const:Nn \c_label_parent_str {.}
\str_const:Nn \c_appendices_label_str {appendices}
\seq_new:N \l_label_seq
\NewDocumentCommand{\Part}{s t^ m >{\TrimSpaces}m}{
\_heading:nnnnn {part} {#1} {#2} {#3} {#4}
\RenewDocumentCommand{\parttitle}{}{#2}
}
\NewDocumentCommand{\Chapter}{s t^ m >{\TrimSpaces}m}{
\_heading:nnnnn {chapter} {#1} {#2} {#3} {#4}
}
\NewDocumentCommand{\Section}{s t^ m >{\TrimSpaces}m}{
\_heading:nnnnn {section} {#1} {#2} {#3} {#4}
}
\NewDocumentCommand{\Subsection}{s t^ m >{\TrimSpaces}m}{
\_heading:nnnnn {subsection} {#1} {#2} {#3} {#4}
}
\NewDocumentCommand{\Subsubsection}{s t^ m >{\TrimSpaces}m}{
\_heading:nnnnn {subsubsection} {#1} {#2} {#3} {#4}
}
\NewDocumentCommand{\Paragraph}{s t^ m >{\TrimSpaces}m}{
\_heading:nnnnn {paragraph} {#1} {#2} {#3} {#4}
}
\NewDocumentCommand{\Subparagraph}{s t^ m >{\TrimSpaces}m}{
\_heading:nnnnn {subparagaph} {#1} {#2} {#3} {#4}
}
\NewDocumentCommand{\Appendices}{}{
\_heading:nnnnn {part} {\c_true_bool} {\c_false_bool} {Appendices} {\c_appendices_label_str}
}
\NewDocumentCommand{\GetCurrentLabel}{}{
\seq_use:NV \l_label_seq \c_label_delimiter_str
}
\NewDocumentCommand{\SetCurrentLabel}{
% #1: Delimiter
O{\c_label_delimiter_str}
% #2: Label string.
>{\TrimSpaces}m
}{
\seq_set_split:Nee \l_label_seq {#1} {#2}
}
\int_new:N \l_level_index_int
\seq_new:N \l_label_temp_seq
\seq_new:N \l_ref_seq
\str_new:N \l_ref_str
\RenewDocumentCommand{\Ref}{
% #1: Star (No star = include section name in reference, star = do not include).
s
% #2: Label string delimiter (defaults to "/").
O{\c_label_delimiter_str}
% #3: Label
>{\TrimSpaces}m
% #4: Parent label symbol (defaults to ".").
O{\c_label_parent_str}
}{
\str_set:Nn \l_ref_str {#3}
\str_if_eq:nnTF {\str_head:N \l_ref_str} {#2} {
\str_replace_all:Nnn \l_ref_str {#4} {#2}
\seq_set_split:Nee \l_ref_seq {#2} {#3}
\int_set:Nn \l_level_index_int {1}
\int_do_until:nNnn {\l_level_index_int} > {\seq_count:N \l_ref_seq} {
\str_if_empty:nTF {\seq_item:Nn \l_ref_seq {\l_level_index_int}} {
\seq_set_item:Nnn \l_ref_seq {
\l_level_index_int
} {
\seq_item:Nn \l_label_seq {\l_level_index_int}
}
}
\int_incr:N \l_level_index_int
}
\str_set:Nn \l_ref_str {\seq_use:Nn \l_ref_seq {#1}}
} {
\str_set:Nn \l_ref_str {#3}
}
\bool_if:nF {#1} {
% Section name
\str_if_eq:nnTF {\seq_item:Nn \l_ref_seq {1}} {\c_appendices_label_str} {
\prop_item:Nn \c_section_names_prop {9}
}{
\prop_item:Nn \c_section_names_prop {\seq_count:N \l_ref_seq - 1}
}
}
\str_put_left:Nn \l_ref_str {\c_label_delimiter_str}
\ref{\l_ref_str}
}
\int_new:N \l_section_level_int
\cs_new:Nn \_heading:nnnnn {
% #1: Section name: part, chapter, section, etc.
% #2: Star (passed to default sectioning command)
% #3: ^ token: suppresses sectioning command.
% #4: Section title
% #5: Label
\prop_get:NnN \c_section_levels_prop {#1} \l_section_level_int
\bool_if:nF {#3} {
\bool_if:nTF {#2} {
\use:c {#1} * {#4}
} {
\use:c {#1} {#4}
}
}
\int_set:Nn \l_level_index_int {\seq_count:N \l_label_seq}
\int_do_while:nNnn {\l_level_index_int} > {\l_section_level_int} {
\seq_pop_right:NN \l_label_seq \l_tmpa_tl
\int_decr:N \l_level_index_int
}
\seq_put_right:Nn \l_label_seq {#5}
\str_set:Nn \l_label_str {\seq_use:Nn \l_label_seq \c_label_delimiter_str}
\str_put_left:Nn \l_label_str {\c_label_delimiter_str}
\exp_args:Ne \label {\l_label_str}
}

View File

@@ -17,10 +17,25 @@
\NewDocumentCommand{\chapappname}{}{Chapter~\thechapter{}:~}
\NewDocumentCommand{\partlabel}{}{\partname{}~\thepart{}:~\parttitle}
\NewDocumentCommand{\FrontMatter}{}{
\pagestyle{plain}
\ifthenelse{\boolean{isElectronic}}{
\pagenumbering{arabic}
}{
\pagenumbering{roman}
}
}
\NewDocumentCommand{\MainMatter}{}{
\pagestyle{fancy}
\pagenumbering{arabic}
}
\NewDocumentCommand{\Appendix}{}{
\appendix
\RenewDocumentCommand{\chapappname}{}{Appendix~\thechapter{}:~}
\RenewDocumentCommand{\partlabel}{}{}
\RenewDocumentCommand{\chapterautorefname}{}{Appendix}
}
\NewDocumentCommand{\BackMatter}{}{

View File

@@ -1,6 +1,6 @@
\NeedsTeXFormat{LaTeX2e}
\ProvidesExplPackage{PrimeTeX/packages/Links}{2025-08-17}{1.0}{
Hyperref configuration and custom label commands.
Hyperref configuration.
}
% Use Hyperref to automatically create links in the document
@@ -26,93 +26,96 @@
}
}
\RenewDocumentCommand{\chapterautorefname}{}{Chapter}
\RenewDocumentCommand{\sectionautorefname}{}{Section}
\RenewDocumentCommand{\subsectionautorefname}{}{Subsection}
\RenewDocumentCommand{\subsubsectionautorefname}{}{Sub-subsection}
\RenewDocumentCommand{\paragraphautorefname}{}{Paragraph}
% \str_new:N \g_SectionLabel_path_str
\bool_new:N \l_SectionLabel_success_bool
% \bool_new:N \l_SectionLabel_success_bool
\int_new:N \l_SectionLabel_slash_total_int
\int_new:N \l_SectionLabel_slash_count_int
\int_new:N \l_SectionLabel_char_count_int
% %\int_new:N \l_SectionLabel_slash_total_int
% % \int_new:N \l_SectionLabel_slash_count_int
% % \int_new:N \l_SectionLabel_char_count_int
% % part:chapter:section:subsection:subsubsection:paragraph:subparagraph
\str_new:N \l_SectionLabel_path_str
\str_new:N \l_SectionLabel_part_str
\str_new:N \l_SectionLabel_chapter_str
\str_new:N \l_SectionLabel_section_str
\str_new:N \l_SectionLabel_subsection_str
\str_new:N \l_SectionLabel_subsubsection_str
\str_new:N \l_SectionLabel_paragraph_str
\str_new:N \l_SectionLabel_subparagraph_str
% part:chapter:section:subsection:subsubsection:paragraph:subparagraph
% \NewDocumentCommand{\SectionLabel}{
% % #1: Section Level. -1=Part, 0=Chapter, 1=Section, 2=Subsection,
% % 3=Sub-subsection, 4=Paragraph, 5=Subparagraph
% O{}
% % Label for the current section.
% m
% }{
% % \str_set:Nn \l_SectionLabel_level_str {#1}
% % \str_set:Nn \l_SectionLabel_label_str {#2}
\NewDocumentCommand{\SectionLabel}{
% #1: Section Level. -1=Part, 0=Chapter, 1=Section, 2=Subsection,
% 3=Sub-subsection, 4=Paragraph, 5=Subparagraph
O{}
% Label for the current section.
m
}{
\str_set:Nn \l_SectionLabel_level_str {#1}
\str_set:Nn \l_SectionLabel_label_str {#2}
\str_case:Nn \l_SectionLabel_level_str {
{} {\int_set:Nn \l_SectionLabel_slash_total_int {0}}
{-1} {\int_set:Nn \l_SectionLabel_slash_total_int {0}}
{0} {\int_set:Nn \l_SectionLabel_slash_total_int {1}}
{1} {\int_set:Nn \l_SectionLabel_slash_total_int {2}}
{2} {\int_set:Nn \l_SectionLabel_slash_total_int {3}}
{3} {\int_set:Nn \l_SectionLabel_slash_total_int {4}}
{4} {\int_set:Nn \l_SectionLabel_slash_total_int {5}}
{5} {\int_set:Nn \l_SectionLabel_slash_total_int {6}}
{part} {\int_set:Nn \l_SectionLabel_slash_total_int {0}}
{chapter} {\int_set:Nn \l_SectionLabel_slash_total_int {1}}
{section} {\int_set:Nn \l_SectionLabel_slash_total_int {2}}
{subsection} {\int_set:Nn \l_SectionLabel_slash_total_int {3}}
{subsubsection} {\int_set:Nn \l_SectionLabel_slash_total_int {4}}
{paragraph} {\int_set:Nn \l_SectionLabel_slash_total_int {5}}
{subparagraph} {\int_set:Nn \l_SectionLabel_slash_total_int {6}}
}
\__SectionLabel_make:
\expandafter\label\expandafter{\l_SectionLabel_path_str}
}
\NewDocumentCommand{\__SectionLabel_make:}{}{
\bool_set_false:N \l_SectionLabel_success_bool
\int_set:Nn \l_SectionLabel_char_count_int {0}
\int_set:Nn \l_SectionLabel_slash_count_int {0}
% \expandafter\label\expandafter{
% \__SectionLabel_make:nn {\__SectionLabel_get_level:n {#1}} #2
% }
% }
\str_map_variable:NNn \l_SectionLabel_path_str \l_tmpa_str {
%\message{{\l_tmpa_str}~slash~count:~\int_use:N \l_SectionLabel_slash_count_int\ ~slash~total:~\int_use:N \l_SectionLabel_slash_total_int^^J}
\int_compare:nNnT
{\l_SectionLabel_slash_count_int} =
{\l_SectionLabel_slash_total_int} {
\str_set:Ne \l_tmpb_str {\str_range:Nnn \l_SectionLabel_path_str {1} {\l_SectionLabel_char_count_int}}
\str_concat:NNN
\l_SectionLabel_path_str
\l_tmpb_str
\l_SectionLabel_label_str
\str_map_break:n {
\message{success}
\bool_set_true:N \l_SectionLabel_success_bool
}
}
\int_incr:N \l_SectionLabel_char_count_int
%\message{\l_tmpa_str}
\str_if_eq:VnT \l_tmpa_str {/} {
%\message{increment}
\int_incr:N \l_SectionLabel_slash_count_int
}
}
% \NewDocumentCommand{\__SectionLabel_get_part:n}{m}
\bool_if:NF \l_SectionLabel_success_bool {
\int_do_until:nNnn
{\l_SectionLabel_slash_count_int} =
{\l_SectionLabel_slash_total_int} {
\str_put_right:Nn \l_SectionLabel_path_str {/}
\int_incr:N \l_SectionLabel_slash_count_int
}
\str_put_right:Ne \l_SectionLabel_path_str {\l_SectionLabel_label_str}
}
}
% \NewDocumentCommand{\__SectionLabel_make:Nnn}{m m m}{ \group_begin:
% \bool_set_false:N \l_SectionLabel_success_bool
% \int_new:N \l_SectionLabel_char_count_int
% \int_set:Nn \l_SectionLabel_char_count_int {0}
% \int_new:N \l_SectionLabel_slash_count_int
% \int_set:Nn \l_SectionLabel_slash_count_int {0}
% \int_const:Ne \l_SectionLabel_slash_total_int {#2}
% \str_const:Ne \l_SectionLabel_label_str {#3}
% \str_new:N \l_SectionLabel_index_char_str
% \str_new:N \l_SectionLabel_temp_path_str
% \str_map_variable:NNn \g_SectionLabel_path_str \l_SectionLabel_index_char_str {
% %\message{{\l_tmpa_str}~slash~count:~\int_use:N \l_SectionLabel_slash_count_int\ ~slash~total:~\int_use:N \l_SectionLabel_slash_total_int^^J}
% \int_compare:nNnT
% {\l_SectionLabel_slash_count_int} =
% {\l_SectionLabel_slash_total_int} {
% \str_set:Ne \l_tmpb_str {\str_range:Nnn #1 {1} {\l_SectionLabel_char_count_int}}
% \str_concat:NNN
% \l_SectionLabel_temp_path_str
% \l_tmpb_str
% \l_SectionLabel_label_str
% \str_map_break:n {
% %\message{success}
% \bool_set_true:N \l_SectionLabel_success_bool
% }
% }
% \int_incr:N \l_SectionLabel_char_count_int
% %\message{\l_tmpa_str}
% \str_if_eq:VnT \l_tmpa_str {/} {
% %\message{increment}
% \int_incr:N \l_SectionLabel_slash_count_int
% }
% }
% \bool_if:NF \l_SectionLabel_success_bool {
% \int_do_while:nNnn
% {\l_SectionLabel_slash_count_int} <
% {\l_SectionLabel_slash_total_int} {
% \str_put_right:Nn \l_SectionLabel_path_str {/}
% \int_incr:N \l_SectionLabel_slash_count_int
% }
% \str_put_right:Ne \l_SectionLabel_path_str {\l_SectionLabel_label_str}
% }
% \group_end: }
% \NewDocumentCommand{\__SectionLabel_get_level:n}{m}{
% \str_case:Nn \tl_to_str:n {#2} {
% {} {0}
% {-1} {0}
% {0} {1}
% {1} {2}
% {2} {3}
% {3} {4}
% {4} {5}
% {5} {6}
% {part} {0}
% {chapter} {1}
% {section} {2}
% {subsection} {3}
% {subsubsection} {4}
% {paragraph} {5}
% {subparagraph} {6}
% }
% }