lvjr / tabularray

Typeset tabulars and arrays with LaTeX3
https://ctan.org/pkg/tabularray
247 stars 22 forks source link

Other ways to distribute space for X columns #365

Open lvjr opened 1 year ago

lvjr commented 1 year ago

Discussed in https://github.com/lvjr/tabularray/discussions/362

Originally posted by **note286** February 4, 2023 ```latex \documentclass{article} \usepackage{showframe} \usepackage{tabularray} \begin{document} \begin{table} \centering \caption{title} \begin{tblr} { colspec = {*{6}{l}}, % colsep = 7.5pt, colsep = 0pt, hlines,vlines } Lorem & ipsum & dolor & sit & amet & consectetur \\ tempor & incididunt & ut & labore & et & dolore \\ quis & nostrud & exercitation & ullamco & laboris & nisi \\ consequat & Duis & aute & irure & dolor & in \\ cillum & dolore & eu & fugiat & nulla & pariatur \\ proident & sunt & in & culpa & qui & officia \\ \end{tblr} \end{table} \end{document} ``` ![image](https://user-images.githubusercontent.com/16207292/216741340-2d3c48fd-8cc9-44b7-acbe-f849eee9c762.png) In the table above, we set `colsep = 0pt`. I hope tblr can automatically complete the setting of `colsep` so that the table width is the specified width, such as `\textwidth`. ![image](https://user-images.githubusercontent.com/16207292/216741412-fbcc4ece-be9b-4071-82cb-70526b83753e.png) I try to set `colsep = 7.5pt` to meet the demand in this example. But this is inconvenient and not accurate. Whether there is a method to automatically complete this step. If you can provide an interface, such as `width = \textwidth, colsep = autofill`, it will be even better. I know the existence of the `X` column format, but I don't want this. In many cases, the proportion between columns is not easy to see, so it is not very convenient to use the `X` column format. All I want is to set `colsep` automatically.
lvjr commented 1 year ago

Issue #100 (about X columns with both co and wd settings) is related.

lvjr commented 1 year ago

@note286 You can use X columns with negative coefficients. The result is similar (but not totally the same) to that of manually setting colsep=7.5pt. See the documentaion of tabu package for how extra space is distributed among X columns.

Also you can use traditional tabular* environment to fill extra spaces. But when there are vertical lines the result is not ideal.

\documentclass{article}
\usepackage{showframe}
\usepackage{tabularray}
\begin{document}

\section{\texttt{tabularray}: minimal width}

\begin{center}
\begin{tblr}{
colspec = {*{6}{l}},
colsep  = 0pt,
hlines,vlines
}
Lorem     & ipsum      & dolor        & sit     & amet    & consectetur \\
tempor    & incididunt & ut           & labore  & et      & dolore      \\
quis      & nostrud    & exercitation & ullamco & laboris & nisi        \\
consequat & Duis       & aute         & irure   & dolor   & in          \\
cillum    & dolore     & eu           & fugiat  & nulla   & pariatur    \\
\end{tblr}
\end{center}

\section{\texttt{tabularray}: calculated \texttt{colsep}}

\begin{center}
\begin{tblr}{
colspec = {*{6}{l}},
colsep  = 7.5pt,
hlines,vlines
}
Lorem     & ipsum      & dolor        & sit     & amet    & consectetur \\
tempor    & incididunt & ut           & labore  & et      & dolore      \\
quis      & nostrud    & exercitation & ullamco & laboris & nisi        \\
consequat & Duis       & aute         & irure   & dolor   & in          \\
cillum    & dolore     & eu           & fugiat  & nulla   & pariatur    \\
\end{tblr}
\end{center}

\section{\texttt{tabularray}: \texttt{X[-1]} columns}

\begin{center}
\begin{tblr}{
colspec = {*{6}{X[l,-1]}},
hlines,vlines
}
Lorem     & ipsum      & dolor        & sit     & amet    & consectetur \\
tempor    & incididunt & ut           & labore  & et      & dolore      \\
quis      & nostrud    & exercitation & ullamco & laboris & nisi        \\
consequat & Duis       & aute         & irure   & dolor   & in          \\
cillum    & dolore     & eu           & fugiat  & nulla   & pariatur    \\
\end{tblr}
\end{center}

\section{\texttt{tabular*}: \texttt{\string\extracolsep}}

\begin{center}
%\setlength\tabcolsep{0pt}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}}|*{6}{l|}}
\hline
Lorem     & ipsum      & dolor        & sit     & amet    & consectetur \\
\hline
tempor    & incididunt & ut           & labore  & et      & dolore      \\
\hline
quis      & nostrud    & exercitation & ullamco & laboris & nisi        \\
\hline
consequat & Duis       & aute         & irure   & dolor   & in          \\
\hline
cillum    & dolore     & eu           & fugiat  & nulla   & pariatur    \\
\hline
\end{tabular*}
\end{center}

\end{document}

image

lvjr commented 1 year ago

I am planning to add a new inner key hspacing which takes the following possible values:

When hspacing=fixed, users can only use positive coefficients and it is better to make sure rules=smash is also applied (#215). We first subtract half of the widths of the first and last vborders from total width and then distribute remaining width to all columns in proportion to their coefficients.

When hspacing=colsep, users can only use positive coefficients and all cell text must be short.

note286 commented 1 year ago

If change the width of paper, I found X[-1] is not not working correctly.

\documentclass{article}
\usepackage[margin=5mm]{geometry}
\usepackage{showframe}
\usepackage{tabularray}
\begin{document}
\section{\texttt{tabularray}: \texttt{X[-1]} columns}
\begin{center}
\begin{tblr}{
colspec = {*{6}{X[l,-1]}},
hlines,vlines
}
Lorem     & ipsum      & dolor        & sit     & amet    & consectetur \\
tempor    & incididunt & ut           & labore  & et      & dolore      \\
quis      & nostrud    & exercitation & ullamco & laboris & nisi        \\
consequat & Duis       & aute         & irure   & dolor   & in          \\
cillum    & dolore     & eu           & fugiat  & nulla   & pariatur    \\
\end{tblr}
\end{center}
\end{document}

image


I read the tabu document and got a certain understanding of using negative number in column X. A positive number means that the column width is fixed, but in the case of a negative number, if the column content width is less than the set width, the natural width will be displayed. This is a example:

\documentclass{article}
\usepackage[margin=5mm]{geometry}
\usepackage{showframe}
\usepackage{tabularray}
\begin{document}
\begin{center}
\begin{tblr}{colspec={X[-1]X[-2]X[1]},hlines,vlines,colsep=0pt}
Lorem ipsum dolor sit amet & consectetur adipisicing elit sed do eiusmod & tempor incididunt ut \\
\end{tblr}
\end{center}

\begin{center}
\begin{tblr}{colspec={X[-1]X[-2]X[1]},hlines,vlines,colsep=0pt}
Lorem ipsum & consectetur adipisicing elit sed do eiusmod labore et dolore magna aliqua Ut enim ad minim veniam & tempor incididunt ut \\
\end{tblr}
\end{center}
\end{document}

image

Question

When there are positive X columns, everything can be understood. When all column formats are negative, how is the width of each column allocated? For example, the following example makes me very confused.

\documentclass{article}
\usepackage[margin=5mm]{geometry}
\usepackage{showframe}
\usepackage{tabularray}
\begin{document}
\begin{center}
\begin{tblr}{colspec={X[-1]X[-1]X[-1]},hlines,vlines,colsep=0pt}
Lorem ipsum & Lorem ipsum Lorem ipsum & Lorem ipsum \\
\end{tblr}
\end{center}
\end{document}

image


Finally, I'm looking forward to the new inner key hspacing.

lvjr commented 1 year ago

When all column formats are negative, how is the width of each column allocated?

Columns with larger natural widths get more extra space.

You could read chapter 11 of tabu documentation if you are still confused.

lvjr commented 1 year ago

A positive number means that the column width is fixed

Sorry, it turns out that my memory on X columns is inaccurate: when all coefficents of X columns are positive, the column widths are fixed. I have updated my comments above accordingly.

lvjr commented 1 year ago

We can also consider what tabulary package does: distributing widths according to natural widths of columns. See discussion https://github.com/lvjr/tabularray/discussions/397.

fredrikpaues commented 1 year ago

We can also consider what tabulary package does: distributing widths according to natural widths of columns. See discussion #397.

Perhaps the column types of the tabulary are so different as to warrant a separate letter? Y?