davidcarlisle / dpctex

Assorted TeX packages
93 stars 30 forks source link

Importing colortbl changes horizontal spacing #48

Closed jovyntls closed 4 months ago

jovyntls commented 5 months ago

\usepackage{colortbl} changes the spacing of tables even when no other commands from the package are used.

Minimal reproduction

\documentclass{article}
\usepackage{colortbl}  % comment this line out to show difference

\begin{document}

\begin{tabular}{|l|c|}
  one&two\\
  three&four
\end{tabular}

$
\begin{array}{ccc}
  ~1~&~2~&~3~&~4~&~5~&~6~&~7~&~8~&~9
\end{array}
$

\end{document}

Without importing colortbl:

image

Importing colortbl using \usepackage{colortbl}:

image

The difference is easier to see when superimposing both PDFs (red is without importing, blue is with \usepackage{colortbl}:

image

Files for reproduction

no_import.log no_import.pdf with_import.log with_import.pdf

davidcarlisle commented 5 months ago

The posted code produces

! Extra alignment tab has been changed to \cr.

which does not show in your log, is the log from the code shown?

davidcarlisle commented 5 months ago

The spacing differences if I change teh second preamble to cccccccccc ae due to array package.##this has a documented changee on spacing of vertical rules. In teh original tabular a vertical rule does not increase teh table width, negative space is added to compensate. With array a vertical rule does add its width.

So some difference are expected in teh first tabular. the second one with no | the difference is less expected, I will trace later, unless @FrankMittelbach looks first.

\documentclass{article}
%\usepackage{array}
\begin{document}

\begin{tabular}{|l|c|}
  one&two\\
  three&four
\end{tabular}

$
\begin{array}{cccccccccc}
  ~1~&~2~&~3~&~4~&~5~&~6~&~7~&~8~&~9
\end{array}
$

\end{document}
FrankMittelbach commented 5 months ago

Isn't the second caused by ~ no longer producing \nobreakspace + an empty ord atom (which it shouldn't have) so that now a space at the end a cell gets dropped and hasn't been dropped in the past (as it should have). ~ is not supposed to be a space that is never dropped, it is supposed to be like \ but without allowing for a line break before the space which makes it equivalent to \ in situations where linebreaks do not happen.

davidcarlisle commented 5 months ago

@FrankMittelbach yes (thanks for looking I couldn't look earlier) but the distinction is not "in the past/now" but "no-array package/array package" so probably we should document difference (or arguably make the kernel tabular treat spaces the same way as array?

FrankMittelbach commented 5 months ago

You are saying that ~ is different depending on array or not array package? That would be odd, yes.

FrankMittelbach commented 5 months ago

that's odd and I guess my explanation can't be right.

davidcarlisle commented 5 months ago

@FrankMittelbach with array there's always an unskip at the end of the cell

with the kernel tabular only \@tabclassz does the unskip \@arrayclassz leaves the skip in place, normally white space in math is ignored anyway so it makes no difference but with an explicit space it does.

FrankMittelbach commented 5 months ago

well, yes, perhaps we should revert to that in array rather than explaining the difference. It does make kind of sense that explicit spaces in a math cell survive. Can't be done the same way as in the core though because basically any column in a tabular could hold math cells not just a complete array environment and it would be really odd if a >$c<$ column would behave differently from a full array env.

davidcarlisle commented 5 months ago

@FrankMittelbach \ifhmode\unskip\fi ?

FrankMittelbach commented 5 months ago

sort of, but as the preamble is build with \xdef that's not quite getting you there. Maybe

\protected\def\textonly@unskip{\ifhmode\unskip\fi}
muzimuzhi commented 5 months ago

I did some testing by applying @FrankMittelbach's idea of \textonly@unskip to \insert@column. It fixed the issue here, and passed the test suite in required/tools directory in latex3/latex2e repo.

\documentclass{article}

\newcommand{\test}{%
  \par\noindent
  \parbox{3cm}{%
    Both sides\\
    $\begin{array}{|c|c|c|}
      ~1~&~2~&~3~
    \end{array}$}
  \parbox{2.8cm}{%
    Left side only\\
    $\begin{array}{|c|c|c|}
      ~1 &~2 &~3
    \end{array}$}
  \parbox{2.8cm}{%
    Right side only\\
    $\begin{array}{|c|c|c|}
      1~& 2~& 3~
    \end{array}$}
  \parbox{2.8cm}{%
    None\\
    $\begin{array}{|c|c|c|}
      1 & 2 & 3
    \end{array}$}
}

% save output of \test without "array"
\setbox0=\hbox{\parbox{\linewidth}{\test}}

\usepackage{array}

\begin{document}

\subsection*{Without array}
\noindent
\usebox0

\subsection*{With array, before}
\test

\subsection*{With array, after}
\makeatletter
\ifdefined\UseTaggingSocket
  % if compiled using pdflatex-dev or the like
  \def\insert@column{%
     \UseTaggingSocket{tbl/cell/begin}%
     \the@toks \the \@tempcnta
     \ignorespaces \@sharp \textonly@unskip
     \the@toks \the \count@ \relax
     \UseTaggingSocket{tbl/cell/end}%
  }
  \def\insert@pcolumn{%
     \UseTaggingSocket{tbl/pcell/begin}%
     \the@toks \the \@tempcnta
     \ignorespaces \@sharp \textonly@unskip
     \the@toks \the \count@ \relax
     \UseTaggingSocket{tbl/pcell/end}%
  }
\else
  \def\insert@column{%
     \the@toks \the \@tempcnta
     \ignorespaces \@sharp \textonly@unskip
     \the@toks \the \count@ \relax}
\fi

\protected\def\textonly@unskip{\ifhmode\unskip\fi}
\makeatother

\test
\end{document}

image

FrankMittelbach commented 4 months ago

@davidcarlisle is it possible to transfer this issue to latex2e or do we need to open a new one for tools?

davidcarlisle commented 4 months ago

@FrankMittelbach can only move within an organization so simplest to open a new one on tools.array I think then close here.

davidcarlisle commented 4 months ago

@jovyntls thanks for your report. As you will see from the above comment Frank has opened the issue at the main LaTeX2e repositiory, for the array package where this difference originates. I'll close here.