yishn / tikzcd-editor

A simple visual editor for creating commutative diagrams.
https://tikzcd.yichuanshen.de/
MIT License
1.86k stars 100 forks source link

"Could not match edge label" error when copying code #45

Closed MMF2 closed 4 years ago

MMF2 commented 4 years ago

When copying the code of a diagram (opening it with the "{ }"-button) and pasting it into another instance of the application I sometimes get an error message "Could not parse code. Reason: Could not match edge label." An example of such code is the following:

\begin{tikzcd}
A \arrow[d, "\overline{g}"'] &  & A\times B \arrow[rrdd, "g"] \arrow[rr, "\pi_2"] \arrow[ll, "\pi_1"'] \arrow[dd, "\overline{g}\times\mathbf{1}_B" description, dashed] &  & B \arrow[d, "\mathbf{1}_B"] \\
C^B                          &  &                                                                                                                                       &  & B                           \\
                             &  & C^B\times B \arrow[llu, "\pi'_1"] \arrow[rru, "\pi'_2"'] \arrow[rr, "{\mathrm{ev}_{B,C}}"']                                           &  & C                          
\end{tikzcd}
yishn commented 4 years ago

@zickgraf Can you please take a look at this?

yishn commented 4 years ago

Here's the permalink to the given diagram for debugging purposes.

zickgraf commented 4 years ago

I just tried to create a minimal example and even the most basic example currently throws an error for me:

\begin{tikzcd}
A \arrow[r] & B
\end{tikzcd}

throws the error

Could not parse code.

Reason: i is not defined

Could this be related to recent changes? Tomorrow I will investigate further.

Edit: the error i is not defined probably arises from this: https://github.com/yishn/tikzcd-editor/commit/6ae530267781f02abf681f7d36b68c36d3daa0b3#r36923167

zickgraf commented 4 years ago

I now have a minimal example and an explanation:

\begin{tikzcd}
A \arrow[r, "{,}"] & B
\end{tikzcd}

The parser splits the arrow definition at ,, even if the comma is inside the label. The same holds for ]. I'm currently not sure how to fix this in a way which does not cause other problems. I will think about this.

yishn commented 4 years ago

Yeah... it's difficult to parse tikzcd syntax correctly without parsing the entirety of LaTeX.

Maybe we can start at the first "{ after the direction directives, iterate over the characters, and use a counter to count up at { or count down at }. We know that the label ends with a " if the counter is 0. Only after we're sure we have the label, can we split at ] for the remaining arrow arguments.

This way, we need to make sure not to count \} and all of these edge cases:

\arrow[r, "{\}}"]
\arrow[r, "{\\}"]
\arrow[r, "{\\\}}"]
zickgraf commented 4 years ago

I would like to avoid having to deal with all the edge cases when escaping stuff. Thus, I have a different suggestion:

If we know where the arrow definition ends (i.e., can find the correct ]), it is easy to match the label by just matching "{.*}" or ".*". Finding the correct ] is also easy as long as the user does not use ] in the label. Luckily, there is \rbrack as a replacement. So I suggest to declare the usage of ] in the label as unsupported and advise users to use \rbrack instead. I will open a PR implementing this any minute.

PS: Sorry for the late reply :(

yishn commented 4 years ago

@zickgraf Don't worry about it, thanks for the pull request!