tabularray icon indicating copy to clipboard operation
tabularray copied to clipboard

[Future Request] Providing a way to create your own variations of the `hline`/`vline` key

Open yarakos95 opened this issue 1 year ago • 5 comments

You can change the thickness, color, dashes, etc. of the ruled line, but the entire document uses only a fixed style of ruled line. It is tedious to specify the style each time using hline/vline.

So it's very useful to be able to define styles for these stylized ruled line.

Motivation

The booktabs library is an important library for creating beautiful tables, but to use this library you need to stick to the basic interfaces.

The tabularray package has the extra interface that separates table style and content, which I feel is a great strength, but the booktabs library interferes with this.

So having this feature (or a key option in the booktabs library) would be very useful, since it would allow you to focus on creating tables in the extra interfaces.

Expected usage

Possible practical examples are the following:

  • Ruled lines provided by the booktabs library
  • Double ruled line

I envision the following usage flow:

  1. Construct a new table/inner key for ruled line using a definition command such as \NewTblrHlineKey/\NewTblrVlineKey.

    %% ruled lines provided by the booktabs library:
    \NewTblrHlineKey{toprule}{ wd = 0.08em }
    \NewTblrHlineKey{bottomrule}{ wd = 0.08em }
    \NewTblrHlineKey{midrule}{ wd = 0.05em }
    \NewTblrHlineKey{cmidrule}{ wd = 0.03em, endpos }
    
    %% double ruled line:
    \NewTblrHlineKey{dbhline}{
      %% Cannot be configured with current hline/inner keys.
      %% For example, it would be useful to have the following properties:
      repeat = 2,
    }
    
  2. With above configuration, it is assumed that you will use it as follows:

    \begin{tblr}{
        toprule{1},
        bottomrule{Z},
        midrule{3},
        cmidrule{2} = {2-3}{}
      }
      %% only tabular contents
    \end{tblr}
    

Comparing the two interfaces

Configure the four keys in preamble as below:

\NewTblrHlineKey{toprule}{ wd = 0.08em }
\NewTblrHlineKey{bottomrule}{ wd = 0.08em }
\NewTblrHlineKey{midrule}{ wd = 0.05em }
\NewTblrHlineKey{cmidrule}{ wd = 0.03em, endpos }

This shows the difference between using the booktabs library and using the extra interface.

booktabs library Extra Interfaces
\begin{tblr}{llll}
  \toprule
    Alpha   & Beta  & Gamma   & Delta \\
  \midrule
    Epsilon & Zeta  & Eta     & Theta \\
  \cmidrule{1-3}
    Iota    & Kappa & Lambda  & Mu    \\
  \cmidrule{2-4}
    Nu      & Xi    & Omicron & Pi    \\
  \bottomrule
\end{tblr}
\begin{tblr}{
    colspec = {llll},
    toprule{1},
    midrule{2},
    cmidrule{3} = {1-3}{},
    cmidrule{4} = {2-4}{},
    bottomrule{Z}
  }
  Alpha   & Beta  & Gamma   & Delta \\
  Epsilon & Zeta  & Eta     & Theta \\
  Iota    & Kappa & Lambda  & Mu    \\
  Nu      & Xi    & Omicron & Pi    \\
\end{tblr}
booktabs library Extra Interfaces
\begin{tblr}{llll}
  \toprule[2pt,purple3]
    Alpha   & Beta  & Gamma  & Delta \\
  \midrule[blue3]
    Epsilon & Zeta  & Eta    & Theta \\
  \cmidrule[azure3]{2-3}
    Iota    & Kappa & Lambda & Mu    \\
  \bottomrule[2pt,purple3]
\end{tblr}
\begin{tblr}{
    colspec = {llll},
    toprule{1} = {2pt, purple3},
    midrule{2} = {blue3},
    cmidrule{3} = {2-3}{azure3},
    bottomrule{Z} = {2pt, purple3}
  }
  Alpha   & Beta  & Gamma  & Delta \\
  Epsilon & Zeta  & Eta    & Theta \\
  Iota    & Kappa & Lambda & Mu    \\
\end{tblr}
booktabs library Extra Interfaces
\begin{tblr}{llll}
  \toprule
    Alpha   & Beta  & Gamma  & Delta \\
  \cmidrule{1-3} \cmidrulemore{2-4}
    Epsilon & Zeta  & Eta    & Theta \\
  \cmidrule{1-3} \morecmidrules \cmidrule{2-4}
    Iota    & Kappa & Lambda & Mu    \\
  \bottomrule
\end{tblr}
\begin{tblr}{
    colspec = {llll},
    toprule{1},
    cmidrule{2} = {1}{1-3}{},
    cmidrule{2} = {2}{2-4}{},
    cmidrule{3} = {1}{1-3}{},
    cmidrule{3} = {2}{2-4}{},
    bottomrule{Z}
  }
  Alpha   & Beta  & Gamma  & Delta \\
  Epsilon & Zeta  & Eta    & Theta \\
  Iota    & Kappa & Lambda & Mu    \\
\end{tblr}
booktabs library Extra Interfaces
\begin{tblr}{llll}
  \toprule
    Alpha   & Beta  & Gamma  & Delta \\
  \cmidrule[lr]{1-2} \cmidrule[lr=-0.4]{3-4}
    Epsilon & Zeta  & Eta    & Theta \\
  \cmidrule[r]{1-2} \cmidrule[l]{3-4}
    Iota    & Kappa & Lambda & Mu    \\
  \bottomrule
\end{tblr}
\begin{tblr}{
    colspec = {llll},
    toprule{1},
    cmidrule{2} = {1-2}{lr},
    cmidrule{2} = {3-4}{lr=-0.4},
    cmidrule{3} = {1-2}{r},
    cmidrule{3} = {3-4}{l},
    bottomrule{Z}
  }
  Alpha   & Beta  & Gamma  & Delta \\
  Epsilon & Zeta  & Eta    & Theta \\
  Iota    & Kappa & Lambda & Mu    \\
\end{tblr}

yarakos95 avatar Apr 09 '25 17:04 yarakos95

I warmly second this FR.

dbitouze avatar Jun 03 '25 14:06 dbitouze

I'm wondering if \NewTblrHlineKey{toprule}{ wd = 0.08em } could be achieved with the experimental public key paths with something like:

\DeclareTblrKeys{hline/inner}{
  toprule .code = {
    hline{#1} = {wd = 0.08em}
  }
}

But, if so, I couldn't make it work...

dbitouze avatar Jun 04 '25 09:06 dbitouze

@dbitouze You can use \DeclareTblrKeys like this:

\documentclass{article}
\usepackage{tabularray}
\usepackage{ninecolors}
\DeclareTblrKeys{table/inner}{
  toprule .meta:n = {
    hline{#1} = {wd = 2pt, fg=purple3}
  },
  bottomrule .meta:n = {
    hline{#1} = {wd = 2pt, fg=purple3}
  },
  midrule .meta:n = {
    hline{#1} = {wd = 1pt, fg=blue3}
  },
  cmidrule .meta:n = {
    hline{#1} = {1-2}{wd = 0.5pt, fg=teal3, lr, endpos},
    hline{#1} = {3-4}{wd = 0.5pt, fg=teal3, lr, endpos}
  },
}
\begin{document}

\begin{tblr}{
  toprule = {1},
  bottomrule = {Z},
  midrule = {2},
  cmidrule = {5}
}
  Head    & Head  & Head   & Head \\
  Alpha   & Beta  & Gamma  & Delta \\
  Epsilon & Zeta  & Eta    & Theta \\
  Iota    & Kappa & Lambda & Mu    \\
  Alpha   & Beta  & Gamma  & Delta \\
  Epsilon & Zeta  & Eta    & Theta \\
  Iota    & Kappa & Lambda & Mu
\end{tblr}

\end{document}
Image

lvjr avatar Jun 05 '25 08:06 lvjr

Nice! But would it be possible, e.g. for toprule, to be able to receive keys just as row? It would be handy to be able to specify e.g.:

toprule{1} = {blue7}

And, then, what about providing these booktabs (the package) equivalents by booktabs (the library) ?

dbitouze avatar Jun 05 '25 10:06 dbitouze

As a test, I implemented it in the following way so that I could create my own hline key.

  1. \NewTblrHlineKey saves the new key name and properties to \l__tblr_hline_special_prop

  2. In \__tblr_table_special_key:nn:

    • Save \l__tblr_key_split_name_str as \l__tblr_key_split_name_tl
    • If \l__tblr_hline_special_prop has the same key as \l__tblr_key_split_name_str, change \l__tblr_key_split_name_str to "hline"
  3. In \tblr_set_hline:nnnn:

    • If \l__tblr_key_split_name_tl is in \l__tblr_hline_special_prop, add the properties stored in \tblr_set_hline:nnn

Specifically, it can be found in the following repository:

https://github.com/yarakos95/tabularray/blob/new-hline/vline-key/tabularray.sty

In this implementation, the toprule key etc. will be available by loading the booktabs library.

yarakos95 avatar Aug 07 '25 17:08 yarakos95