Skip to content

Commit

Permalink
Add option "table-model-setup" (fixes #659)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephwright committed Jul 27, 2023
1 parent d1f43d1 commit e0070ec
Show file tree
Hide file tree
Showing 7 changed files with 815 additions and 60 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -32,6 +32,8 @@ Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to
- `range-open-bracket`
(see issue
[\#262](https://github.com/josephwright/siunitx/issues/262))
- Option `table-model-setup` (see issue
[\#659](https://github.com/josephwright/siunitx/issues/659))

### Changed
- Support bracing of decimal marker in tables as an alternative
Expand Down
60 changes: 46 additions & 14 deletions siunitx-table.dtx
Expand Up @@ -190,6 +190,15 @@
% the same size.
% \end{function}
%
% \begin{function}{table-model-setup}
% \begin{syntax}
% |table-model-setup| = \meta{commands}
% \end{syntax}
% Additional commands to be inserted when using the |table-format| to
% create a model for alignment of cells. Typically this will be used to
% handle variable-width fonts in columns. The standard setting is empty.
% \end{function}
%
% \begin{function}{table-number-alignment}
% \begin{syntax}
% |table-number-alignment| = |center|\verb"|"|left|\verb"|"|right|
Expand Down Expand Up @@ -818,7 +827,12 @@
% \end{macro}
%
% \begin{variable}
% {\l_@@_auto_round_bool, \l_@@_align_mode_tl, \l_@@_align_number_tl}
% {
% \l_@@_auto_round_bool,
% \l_@@_model_setup_tl ,
% \l_@@_align_mode_tl ,
% \l_@@_align_number_tl
% }
% Options for tables with defined space.
% \begin{macrocode}
\keys_define:nn { siunitx }
Expand All @@ -842,6 +856,8 @@
\exp_args:NV \@@_generate_model:n \l_@@_model_tl
\tl_set:Nn \l_@@_align_mode_tl { format }
} ,
table-model-setup .tl_set:N =
\l_@@_model_setup_tl ,
table-number-alignment .choices:nn =
{ center , left , right }
{ \tl_set_eq:NN \l_@@_align_number_tl \l_keys_choice_tl }
Expand Down Expand Up @@ -1174,7 +1190,12 @@
% \@@_print_format_auxxii:w ,
% \@@_print_format_auxxiii:w
% }
% \begin{macro}{\@@_print_format_box:Nn, \@@_print_format_box:No}
% \begin{macro}
% {
% \@@_print_format_box:Nn, \@@_print_format_box:No ,
% \@@_print_model_box:Nn, \@@_print_model_box:No
% }
% \begin{macro}{\@@_print_format_box:nNn, \@@_print_format_box:VNn}
% \begin{macro}{\@@_print_format_after:N}
% \begin{macro}{\@@_print_none:nnn}
% \begin{macrocode}
Expand Down Expand Up @@ -1325,7 +1346,7 @@
#1 \q_nil #2 \q_mark #3 \q_nil #4 \q_stop
{
\@@_color_check:w #3 \q_nil \q_stop
\@@_print_format_box:Nn \l_@@_tmp_box {#1}
\@@_print_model_box:Nn \l_@@_tmp_box {#1}
\bool_if:NTF \l_@@_align_before_bool
{
\hbox_set_to_wd:Nnn \l_@@_integer_box
Expand Down Expand Up @@ -1365,7 +1386,7 @@
\cs_new_protected:Npn \@@_print_format_auxii:w
#1 \q_nil #2 \q_nil #3 \q_mark #4 \q_nil #5 \q_nil #6 \q_stop
{
\@@_print_format_box:Nn \l_@@_tmp_box {#1#2}
\@@_print_model_box:Nn \l_@@_tmp_box {#1#2}
\dim_set:Nn \l_@@_tmp_dim { \box_wd:N \l_@@_tmp_box }
\@@_print_format_box:Nn \l_@@_tmp_box {#4#5}
\dim_compare:nNnT { \box_wd:N \l_@@_tmp_box } > \l_@@_tmp_dim
Expand Down Expand Up @@ -1437,7 +1458,7 @@
\cs_new_protected:Npn \@@_print_format_auxiii:w
#1 \q_nil #2 \q_nil #3 \q_mark #4 \q_nil #5 \q_nil #6 \q_stop
{
\@@_print_format_box:No \l_@@_tmp_box { \use_none:n #1#2 }
\@@_print_model_box:No \l_@@_tmp_box { \use_none:n #1#2 }
\@@_print_format_box:No \l_@@_decimal_box { \use_none:n #4#5 }
\dim_set:Nn \l_@@_carry_dim
{
Expand Down Expand Up @@ -1506,7 +1527,7 @@
#1 \q_nil #2 \q_nil #3 \q_mark
#4 \q_nil #5 \q_nil #6 \q_stop
{
\@@_print_format_box:Nn \l_@@_tmp_box {#1#2}
\@@_print_model_box:Nn \l_@@_tmp_box {#1#2}
\hbox_set_to_wd:Nnn \l_@@_tmp_box
{
\box_wd:N \l_@@_uncert_box
Expand Down Expand Up @@ -1536,7 +1557,7 @@
#1 \q_nil #2 \q_mark
#3 \q_nil #4 \q_stop
{
\@@_print_format_box:Nn \l_@@_tmp_box { { } #1 }
\@@_print_model_box:Nn \l_@@_tmp_box { { } #1 }
\dim_set:Nn \l_@@_tmp_dim { \box_wd:N \l_@@_tmp_box }
\@@_print_format_box:Nn \l_@@_tmp_box { { } #3 }
\@@_print_format_auxxii:w #2 \q_mark #4 \q_stop
Expand All @@ -1563,15 +1584,15 @@
{
\tl_if_blank:nF {#2}
{
\@@_print_format_box:Nn \l_@@_tmp_box { { } #1#2 }
\@@_print_model_box:Nn \l_@@_tmp_box { { } #1#2 }
\dim_set:Nn \l_@@_tmp_dim { \box_wd:N \l_@@_tmp_box }
\@@_print_format_box:Nn \l_@@_tmp_box
{
\bool_lazy_and:nnT
{ \l_@@_align_exponent_bool }
{ \tl_if_blank_p:n {#3} }
{
\@@_print_format_box:Nn \l_@@_tmp_box { { } #1 { } }
\@@_print_model_box:Nn \l_@@_tmp_box { { } #1 { } }
\@@_skip:n { \box_wd:N \l_@@_tmp_box }
}
{ } #3#4
Expand All @@ -1581,17 +1602,27 @@
}
% \end{macrocode}
% A simple auxiliary to avoid relatively expensive use of the print routine
% for empty parts.
% for empty parts. There is a separate function for the model as this allows
% for the case where different font widths are in use.
% \begin{macrocode}
\cs_new_protected:Npn \@@_print_format_box:Nn #1#2
{ \@@_print_format_box:nNn { } #1 {#2} }
\cs_generate_variant:Nn \@@_print_format_box:Nn { No }
\cs_new_protected:Npn \@@_print_model_box:Nn #1#2
{ \@@_print_format_box:VNn \l_@@_model_setup_tl #1 {#2} }
\cs_generate_variant:Nn \@@_print_model_box:Nn { No }
\cs_new_protected:Npn \@@_print_format_box:nNn #1#2#3
{
\hbox_set:Nn #1
\hbox_set:Nn #2
{
\tl_if_blank:nF {#2}
{ \siunitx_print_number:n {#2} }
\tl_if_blank:nF {#3}
{
#1
\siunitx_print_number:n {#3}
}
}
}
\cs_generate_variant:Nn \@@_print_format_box:Nn { No }
\cs_generate_variant:Nn \@@_print_format_box:nNn { V }
% \end{macrocode}
% A common routine for placing material after the decimal marker and
% \enquote{shuffling}.
Expand Down Expand Up @@ -1655,6 +1686,7 @@
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \subsection{Standard settings for module options}
%
Expand Down
55 changes: 36 additions & 19 deletions siunitx.tex
Expand Up @@ -2766,19 +2766,20 @@ \subsection{Tabular material}
Type &
\multicolumn{1}{l@{}}{Default} \\
\midrule
table-align-comparator & Switch & true \\
table-align-exponent & Switch & true \\
table-align-text-after & Switch & true \\
table-align-text-before & Switch & true \\
table-align-uncertainty & Switch & true \\
table-alignment & Meta & center \\
table-alignment-mode & Choice & marker \\
table-auto-round & Switch & false \\
table-column-width & Length & 0pt \\
table-fixed-width & Switch & false \\
table-format & Special & 2.2 \\
table-number-alignment & Choice & center \\
table-text-alignment & Choice & center \\
table-align-comparator & Switch & true \\
table-align-exponent & Switch & true \\
table-align-text-after & Switch & true \\
table-align-text-before & Switch & true \\
table-align-uncertainty & Switch & true \\
table-alignment & Meta & center \\
table-alignment-mode & Choice & marker \\
table-auto-round & Switch & false \\
table-column-width & Length & 0pt \\
table-fixed-width & Switch & false \\
table-format & Special & 2.2 \\
table-model-setup & Literal & \meta{empty} \\
table-number-alignment & Choice & center \\
table-text-alignment & Choice & center \\
\bottomrule
\end{tabular}
\end{table}
Expand Down Expand Up @@ -2896,6 +2897,12 @@ \subsection{Tabular material}
zero). Setting the \opt{table-format} option also resets
\opt{table-alignment-mode} to \opt{format}.

\DescribeOption{table-model-setup}
In some tables (particularly those using bold extended fonts), it may be
necessary to insert additional information when creating the \enquote{model}.
This is handled using the \opt{table-model-setup} key. An example of the use of
this key is given in Section~\ref{sec:hint:heading}.

Space for material before and after the \texttt{S} column can be reserved by
giving model text as part of the \opt{table-format} key. This is then used to
provide the necessary gap while maintaining alignment (Table~\ref{tab:S:ends}).
Expand Down Expand Up @@ -3855,20 +3862,30 @@ \subsection{Tables with heading rows%
demonstrated in Section~\ref{sec:hint:expanding}), then to use this macro to
make the heading cells bold. This approach is illustrated in
Table~\ref{tab:xmpl:headers}, along with the use of \cs{rowcolor} to provide a
background color.
background color. Some typical \LaTeX{} font arrangements use a wider
(extended) width for the bold font compared with the normal font. This will
disturb the alignment if the \opt{table-format} is not symmetrical. For these
cases, the \opt{table-model-setup} key may be used to apply the wider font to
the \enquote{model} used in ensuring alignment.
\begin{LaTeXdemo}[code and float]
\begin{table}
\caption{Header row in a table.%
\label{tab:xmpl:headers}}
\robustify\bfseries
\centering
\sisetup{text-series-to-math}
\begin{tabular}
{@{}S[text-series-to-math, table-format = 3.3]@{}}
{
@{}
S[table-format = 3.3]
S[table-format = 3.6, table-model-setup = \bfseries]
@{}
}
\rowcolor[gray]{0.9}
\bfseries 123.456 \\
23.45 \\
123.4 \\
3.456 \\
\bfseries 123.456 & \bfseries 123.456789 \\
23.45 & 23.45 \\
123.4 & 123.4 \\
3.456 & 3.456 \\
\end{tabular}
\end{table}
\end{LaTeXdemo}
Expand Down

0 comments on commit e0070ec

Please sign in to comment.