Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support & in matrices without catcode change #1081

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

gabor-braun
Copy link
Contributor

  1. Support & with its usual catcode as cell separator in matrices,
    and no longer change the catcode in TikZ
  2. Correctly handle column spacing between origins between columns not specified in the first row.
  3. Simplify some code in pgfmodulematrix.code.tex, most notably that of empty cells.

The documentation has been adjusted to not promote anything else as & as cell separator. All examples now use &, except for style /tikz/ampersand replacment.
The macro \pgfmatrixnextcell is still documented.

Two examples have been slightly altered to showcase that between origins work between columns not specified in the first row.

Motivation for this change

Supporting & gets rid of the documented problems when matrices appear at less common places or have content with &.

The optional argument of & is not available immediately, so
the code for column spacing needs changing anyway.
It was easier not to reproduce an undocumented, unexpected behavior. For explicitness, this change occurs early in the branch.

Simplification are mostly taking the opportunity, and not necessary for the changes above, and appear near end of the branch. Empty cell handling needed change anyway to work correctly for empy cells ending with &.

Testing performed

The commits have been tested with som TikZ pictures, mostly from the documentation. With the last commit the LuaTeX version of the PGF documentation compiles succesfully.
It has not been built for intermediate versions.

Checklist

@hmenke
Copy link
Member

hmenke commented Dec 2, 2021

This is an interesting proposal but it needs very careful testing because there are a lot of downstream consumers of this code like tikz-cd.

Please also sign-off on your commits. It can be done in batch using

git remote update
git rebase --exec 'git commit --amend --no-edit -n -s' @{upstream}

@gabor-braun
Copy link
Contributor Author

gabor-braun commented Dec 3, 2021

All commits should be signed off now. (Done with git rebase --signoff.)

The manual of tikz-cd version 0.9f successfully compiles under lualatex and xelatex with these changes. (I couldn't compile it with pdflatex even with the upstream version of pgf.)

I understand that lot more testing is necessary.

Column separation is handled by modifying the bounding box of cells.
Adjust this to handle the case when [between origins] is specified
in later rows, like

\begin{tikzpicture}
  \matrix[column sep={5mm,between origins}, nodes=draw]{
    \node{1}; & \node(2){2}; \\
    \node{3}; & \node{4}; & \node(5){5}; \\
  };
  \draw (5) -- (2);
\end{tikzpicture}

Signed-off-by: Gábor Braun <[email protected]>
Upcoming changes require that \pgfmatrixendrow start with &,
and column separation will need to be saved as well,
as it will be processed later outside the cell group.

These are to support constructs like (see bug pgf-tikz#503)

\matrix[every odd row/.style={row sep=1cm}] {...};

Signed-off-by: Gábor Braun <[email protected]>
This is a step towards supporting & in place of \pgfmatrixnextcell,
as & is not able to immediately process any argument.

Move code from \pgf@matrix@endcell to \pgfmatrixnextcell.
Remove \pgf@matrix@process@nextcell@options, no longer used.

Notable aspects of the move needing care:
1) The test \ifpgf@matrix@last@cell@in@row has been omitted,
   as no longer necessary, and no longer guaranteed to be correct.
2) The test \pgfmatrixcurrentcolumn < \pgf@matrix@numberofcolumns
   still yields the correct result, even though
   \pgf@matrix@numberofcolumns might have a different value.
3) \pgf@matrix@maxx might have a different value, but needs no change
   in code.

Signed-off-by: Gábor Braun <[email protected]>
Last commit made it a noop code.

Signed-off-by: Gábor Braun <[email protected]>
Detect it by the single token \pgf@matrix@endcell
(brought in by & starting a new cell), which is easier to check than
the two tokens \let\pgf@matrix@signal@cell@end.

Signed-off-by: Gábor Braun <[email protected]>
Think of "\pgfmatrixnextcell &" or "\pgfmatrixendrow &".

The usual trick for this is surround looking ahead of next character
by "\ifnum\iffalse{\fi0=`}\fi" and "\ifnum`{=0}\fi",
but here we use variants.

\pgfmatrixnextcell:
  As the look ahead does not occur in math mode, do it in a TeX group.
  This defends again & or \cr ending up in a (hidden) temporary
  control sequence for a long time.

\pgfmatrixendrow:
  Look for an optional argument in the \noalign group, where the trick
  is not necessary.

Signed-off-by: Gábor Braun <[email protected]>
There is no longer an ampersand "wrong catcode".

In documentation promote & as cell separator, use it in all examples,
and remove limitation due to catcode of ampersand.
Still document \pgfmatrixnextcell, which is now simply &.

The main technical idea is adding "\aftergroup\pgf@matrix@nextcell"
to the \halign preamble.  The macro \pgf@matrix@nextcell will be added
after the cell group, i.e., right after "&" in user input, so TeX will
see \pgf@matrix@nextcell as the first token of the next table cell.
Now \pgf@matrix@nextcell does what \pgfmatricnextcell did before.

Signed-off-by: Gábor Braun <[email protected]>
Use \pgf@matrix@maxxCOLUMN instead, which might have a different value
but doesn't need extra global assignments.

Signed-off-by: Gábor Braun <[email protected]>
Temporarily redefine \pgfmatrixendcode in empty cells, which is a
much simpler way to omit end code.

Signed-off-by: Gábor Braun <[email protected]>
Since one part is extremely short.

Signed-off-by: Gábor Braun <[email protected]>
This is to simplify code without changing functionality.
Move \pgfmatrix@init@row out of halign header to this end.

Signed-off-by: Gábor Braun <[email protected]>
Make less dimension calculations, glue insertions, assignments
for the same result.

Signed-off-by: Gábor Braun <[email protected]>
Simplify code to handle column separation in pgf matrices:
change \pgf@picminx only once per matrix cell for this purpose.

Signed-off-by: Gábor Braun <[email protected]>
Include tests for packages: tikz-cd, tikz-dependency, beamer
(deliberately ignoring advice to not use & as cell separator).

Omit dvisvgm result for beamer: it contains complicated dvips specials.

Signed-off-by: Gábor Braun <[email protected]>
@gabor-braun
Copy link
Contributor Author

I have added some tests. On my machine they run successfully, but some fail on GitHub. How to access the directory build/test on GitHub, where l3build puts test results?

@muzimuzhi
Copy link
Member

I have added some tests. On my machine they run successfully, but some fail on GitHub. How to access the directory build/test on GitHub, where l3build puts test results?

I've opened PR #1114 to upload *.diff files as an artifact. Before it's merged (and your branch matrix-ampersand rebased), you can download the zipped *.diff files from https://github.com/muzimuzhi/pgf/actions/runs/1597478038.

From the content of *.diff files,

  • some simple lines might come from different latex2e versions
  • dvips output seems OS specific
  • dvisvgm generates rather different output which I can't even guess why

@gabor-braun
Copy link
Contributor Author

Thank you for the diffs, they have helped.

For dvisvgm, the font numbers differ, e.g., \FONT24 vs \FONT16. If these are LuaTeX's \fontid, then they are not supposed to be stable. dvisvgm is the only engine using LuaTeX with dvi output.
At least I see such entries with the command line

lualatex --output-format=dvi '\documentclass{article}\showoutput \begin{document} abc \end{document}'

Other egines seem to print TeX names instead: \OT1/cmr/m/n/10 and similar.

|\pgfmatrixendrow| inside the \meta{matrix cells}. It suffices that
the macros inside \meta{matrix cells} expand to these macros
the macros inside \meta{matrix cells} expand to these
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that the row is changed anyway, we can directly remove one of the remaining "double space" instances :)

Suggested change
the macros inside \meta{matrix cells} expand to these
the macros inside \meta{matrix cells} expand to these

@hmenke
Copy link
Member

hmenke commented Feb 1, 2022

This PR is blocked by #1116.

Comment on lines -193 to +190
\halign\bgroup%
\pgf@matrix@init@row%
\halign\bgroup%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. Why is \pgf@matrix@init@row called outside of the \halign?

Comment on lines -4062 to -4067
{%
\catcode`\&=13
\gdef\tikz@matrix@make@active@ampersand{%
\def\tikz@matrix@make@active@ampersand{%
\ifx\tikz@ampersand@replacement\pgfutil@empty%
\catcode`\&=13%
\let&=\pgfmatrixnextcell%
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need to set up \let&=\pgfmatrixnextcell in case someone set & active outside of the matrix. Existing code might do this to work around tokenization problems. That way an active & will expand to non-active & inside the matrix.

{%
  \catcode`\&=13
  \gdef\tikz@matrix@make@active@ampersand{%
    \ifx\tikz@ampersand@replacement\pgfutil@empty%
      %\catcode`\&=13% <--- removed
      \let&=\pgfmatrixnextcell%
    \else%
      \expandafter\let\tikz@ampersand@replacement=\pgfmatrixnextcell%
    \fi%
  }%
}%

@hmenke
Copy link
Member

hmenke commented Oct 23, 2023

Sorry, I had already reviewed this a year ago, but forgot to hit send.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants