ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* [NTG-context] Tikz CD
@ 2024-11-25 15:07 Aditya Mahajan
  2024-11-25 15:50 ` [NTG-context] " Hans Hagen
  0 siblings, 1 reply; 3+ messages in thread
From: Aditya Mahajan @ 2024-11-25 15:07 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hi,

There was a recent question about what appeared to be commutative diagrams, and I wanted to reply saying that one option is to use tikzcd module. However, testing showed that it fails.

This is what tikzlibrarycd.code.tex does:

% ConTeXt-specific stuff
\pgfutil@IfUndefined{starttikzpicture}{}{%
  \def\starttikzcd{\tikzcd}
  \def\stoptikzcd{\endtikzcd}
  \tikzcdset{
    every matrix/.append code={%
      \def\NC{\pgfmatrixnextcell}%
      \def\NR{\pgfmatrixendrow}}}
}


where 

\def\tikzcd@[#1]{%
  \tikzpicture[/tikz/commutative diagrams/.cd,every diagram,#1]%
  ...}

So, it fails because \tikzpicture is being called with wrong catcodes. It can be fixed in the same manner as we patched tikzpicture as shown by the following MWE:

\usemodule[tikz]
\usetikzlibrary[cd]

\permanent\protected\def\starttikzcd%
  {\starttikzinput
   \hcontainer\bgroup
   \tikzcd}

\permanent\protected\def\stoptikzcd
  {\endtikzcd
   \egroup
   \stoptikzinput}

\starttext
\startformula
  \starttikzcd
    x \arrow[d] \NC A \arrow[d] \NC D \NR
    y \NC B \NC D \NR
  \stoptikzcd
\stopformula
\stoptext


What is the best way to go about this? I can try to ask the 

Should we include a wrapper for tikzcd (as we do for circuitikz) or should I try to push a patch upstream. 

BTW, why do we need a \hcontainer in the above code?


Aditya


___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [NTG-context] Re: Tikz CD
  2024-11-25 15:07 [NTG-context] Tikz CD Aditya Mahajan
@ 2024-11-25 15:50 ` Hans Hagen
  2024-11-26  6:39   ` Aditya Mahajan
  0 siblings, 1 reply; 3+ messages in thread
From: Hans Hagen @ 2024-11-25 15:50 UTC (permalink / raw)
  To: ntg-context

On 11/25/2024 4:07 PM, Aditya Mahajan wrote:
> Hi,
> 
> There was a recent question about what appeared to be commutative diagrams, and I wanted to reply saying that one option is to use tikzcd module. However, testing showed that it fails.
> 
> This is what tikzlibrarycd.code.tex does:
> 
> % ConTeXt-specific stuff
> \pgfutil@IfUndefined{starttikzpicture}{}{%
>    \def\starttikzcd{\tikzcd}
>    \def\stoptikzcd{\endtikzcd}
>    \tikzcdset{
>      every matrix/.append code={%
>        \def\NC{\pgfmatrixnextcell}%
>        \def\NR{\pgfmatrixendrow}}}
> }
> 
> 
> where
> 
> \def\tikzcd@[#1]{%
>    \tikzpicture[/tikz/commutative diagrams/.cd,every diagram,#1]%
>    ...}
> 
> So, it fails because \tikzpicture is being called with wrong catcodes. It can be fixed in the same manner as we patched tikzpicture as shown by the following MWE:

sounds ok to me

> \usemodule[tikz]
> \usetikzlibrary[cd]
> 
> \permanent\protected\def\starttikzcd%
>    {\starttikzinput
>     \hcontainer\bgroup
>     \tikzcd}
> 
> \permanent\protected\def\stoptikzcd
>    {\endtikzcd
>     \egroup
>     \stoptikzinput}
> 
> \starttext
> \startformula
>    \starttikzcd
>      x \arrow[d] \NC A \arrow[d] \NC D \NR
>      y \NC B \NC D \NR
>    \stoptikzcd
> \stopformula
> \stoptext
> 
> What is the best way to go about this? I can try to ask the

i assume youy send me a patch(ed file)

> Should we include a wrapper for tikzcd (as we do for circuitikz) or should I try to push a patch upstream.

wrapper

> BTW, why do we need a \hcontainer in the above code?

it makes sure we don't go into the box to mess around with context 
things (who knows what tikz adds in there that can interfere)

Hans

-----------------------------------------------------------------
                                           Hans Hagen | PRAGMA ADE
               Ridderstraat 27 | 8061 GH Hasselt | The Netherlands
        tel: 038 477 53 69 | www.pragma-ade.nl | www.pragma-pod.nl
-----------------------------------------------------------------
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [NTG-context] Re: Tikz CD
  2024-11-25 15:50 ` [NTG-context] " Hans Hagen
@ 2024-11-26  6:39   ` Aditya Mahajan
  0 siblings, 0 replies; 3+ messages in thread
From: Aditya Mahajan @ 2024-11-26  6:39 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 1125 bytes --]

On Mon, 25 Nov 2024, Hans Hagen wrote:

> On 11/25/2024 4:07 PM, Aditya Mahajan wrote:
> > What is the best way to go about this? I can try to ask the
> 
> i assume youy send me a patch(ed file)

We need to add the following at the end of m-tikz.mkxl (I am no longer testing tikz for MkIV, so no patch for that). 

\startsetups tikz:hacks:tikzcd
\permanent\protected\def\starttikzcd%
  {\starttikzinput
   \hcontainer\bgroup
   \tikzcd}

\permanent\protected\def\stoptikzcd
  {\endtikzcd
   \egroup
   \stoptikzinput}
\stopsetups

\installtikzlibraryhacks{cd}{}{\directsetup{tikz:hacks:tikzcd}}

A patched file is also attached. Then the following code works fine:

\usemodule[tikz]
\usetikzlibrary[cd]

\starttext
\startformula
  \starttikzcd
    x \arrow[d] \NC A \arrow[d] \NC D \NR
    y \NC B \NC D \NR
  \stoptikzcd
\stopformula
\stoptext

I also wanted to add tikzcd to mtx-install-imp-tikz, but a TDS zip is not available at https://ctan.org/tex-archive/graphics/pgf/contrib/tikz-cd. I will check why that is not the case later, and if a CTAN TDS is available, send a patch to include it in install-imp-tikz. 

Aditya

[-- Attachment #2: Type: text/plain, Size: 8420 bytes --]

%D \module
%D   [       file=m-tikz,
%D        version=2021.07.12,
%D          title=\CONTEXT\ Extra Modules,
%D       subtitle=TIKZ support,
%D         author=Hans Hagen,
%D           date=\currentdate,
%D      copyright={PRAGMA ADE \& \CONTEXT\ Development Team}]
%C
%C This module is part of the \CONTEXT\ macro||package and is
%C therefore copyrighted by \PRAGMA. See mreadme.pdf for
%C details.

%D A fixed variant of the t-tikz module distributed with tikz. For practical reasons
%D per 2021 we split the module for \MKIV\ and \LMTX. (Maybe some day I'll optimize
%D TIKZ a bit.)
%D
%D All these t-* modules in the tikz distribution make no sense as we can just load
%D them here which is way cleaner. There is also some mkii filename mapping going on
%D that is not needed on todays systems (windows has no 8.3 limitation and on unix
%D tds finally adopted a case insensitive approach.)

%D Just in case someone does a hard input ...

\ifdefined\pgfdefined    \endinput \fi
\ifdefined\pgfcoredefined\endinput \fi

%D ... but normally we end up here:

\startmodule[tikz]

\ifdefined\hoffset \else
    \newdimen\hoffset
    \newdimen\voffset
\fi

\ifdefined\pdflastxpos \else
    \unprotect
        \frozen\overloaded\protected\def\pdflastxpos{\numexpr\clf_lastxpos\relax}
        \frozen\overloaded\protected\def\pdflastypos{\numexpr\clf_lastypos\relax}
    \protect
\fi

\ifdefined\pdfstrcmp\else

    \startluacode
        interfaces.implement {
            name      = "pdfstrcmp",
            arguments = "2 strings",
            usage     = "value",
            public    = true,
            actions   = function(a,b)
                return
                    tokens.values.integer,
                    (a < b and -1) or (a > b and 1) or 0
            end
        }
    \stopluacode

\fi

\newcatcodetable \tikzcatcodes

\startcatcodetable \tikzcatcodes
    \catcode\tabasciicode             \spacecatcode
    \catcode\endoflineasciicode       \endoflinecatcode
    \catcode\formfeedasciicode        \endoflinecatcode
    \catcode\spaceasciicode           \spacecatcode
    \catcode\endoffileasciicode       \ignorecatcode
    \catcode\circumflexasciicode      \superscriptcatcode
    \catcode\underscoreasciicode      \subscriptcatcode
    \catcode\ampersandasciicode       \alignmentcatcode
    \catcode\backslashasciicode       \escapecatcode
    \catcode\leftbraceasciicode       \begingroupcatcode
    \catcode\rightbraceasciicode      \endgroupcatcode
    \catcode\dollarasciicode          \mathshiftcatcode
    \catcode\hashasciicode            \parametercatcode
    \catcode\commentasciicode         \commentcatcode
    \catcode\atsignasciicode          \lettercatcode
    \catcode\exclamationmarkasciicode \othercatcode
    \catcode\questionmarkasciicode    \lettercatcode
    \catcode\tildeasciicode           \activecatcode
    \catcode\barasciicode             \othercatcode
\stopcatcodetable

\newinteger\tikzinputlevel

\permanent\protected\def\starttikzinput
  {\advanceby\tikzinputlevel\plusone
   \ifcase\tikzinputlevel\or
     \pushoverloadmode
     \pushcatcodetable
     \setcatcodetable\tikzcatcodes
     \pushmacro\meaning
     \let\meaning\meaningless
     \autoparagraphmode\zerocount
  \fi}

\permanent\protected\def\stoptikzinput
  {\autoparagraphmode\plusone
   \ifcase\tikzinputlevel\or
     \popcatcodetable
     \popmacro\meaning
     \popoverloadmode
   \fi
   \advanceby\tikzinputlevel\minusone}

\permanent\protected\def\tikzinputfile#1%
  {\starttikzinput
   \input{#1}\relax
   \stoptikzinput}

\permanent\protected\def\tikzerrormessage#1#2#3%
  {\writestatus{#1}{#2}}

% For now we need this but we need to educate the user to wrap settings in the
% wrappers. So some day the next line will go. I need to check what commands are
% possible outside a picture.

\overloadmode\zerocount

\newtoks\everytikzpicture
\newtoks\everyinsidetikzpicture

% \appendtoks
%     \resetcharacterspacing
% \to \everytikzpicture

\permanent\protected\def\starttikzsettings
  {\pushoverloadmode
   \pushcatcodetable
   \setcatcodetable\tikzcatcodes
   \pushmacro\meaning
   \let\meaning\meaningless
   \autoparagraphmode\zerocount}

\permanent\protected\def\stoptikzsettings
  {\autoparagraphmode\plusone
   \popmacro\meaning
   \popcatcodetable
   \popoverloadmode}

\permanent\tolerant\protected\def\starttikzpicture[#S#1]%
  {\dontleavehmode
   \hcontainer\bgroup
   \setcatcodetable\tikzcatcodes
   \the\everytikzpicture
   \autoparagraphmode\zerocount
   \pushmacro\meaning
   \let\meaning\meaningless
 % \pushoverloadmode
   \ifdefined\PackageError\else \let\PackageError\tikzerrormessage \fi
   \tikzpicture[#1]%
   \the\everyinsidetikzpicture}

\permanent\protected\def\stoptikzpicture
  {\endtikzpicture
 % \popoverloadmode
   \popmacro\meaning
   \egroup}

\let\pgfdefined    \relax
\let\pgfcoredefined\relax

\tikzinputfile{pgfutil-common.tex}
\tikzinputfile{pgfutil-context.def}
\tikzinputfile{pgfrcs.code.tex}
\tikzinputfile{pgfsys.code.tex}
\tikzinputfile{pgfkeys.code.tex}
\tikzinputfile{pgfsyssoftpath.code.tex}
\tikzinputfile{pgfsysprotocol.code.tex}
\tikzinputfile{pgfcore.code.tex}
\tikzinputfile{pgffor.code.tex}
\tikzinputfile{pgfmoduleplot.code.tex}
\tikzinputfile{tikz.code.tex}

\let\startpgfpicture             \pgfpicture              \let\stoppgfpicture            \endpgfpicture
\let\startpgfscope               \pgfscope                \let\stoppgfscope              \endpgfscope
\let\startpgflowlevelscope       \pgflowlevelscope        \let\stoppgflowlevelscope      \endpgflowlevelscope
\let\startpgfinterruptpath       \pgfinterruptpath        \let\stoppgfinterruptpath      \endpgfinterruptpath
\let\startpgfinterruptpicture    \pgfinterruptpicture     \let\stoppgfinterruptpicture   \endpgfinterruptpicture
\let\startpgfinterruptboundingbox\pgfinterruptboundinbox  \let\stoppgfinterruptboudingbox\endpgfinterruptboundingbox

\let\normalusepgfmodule  \usepgfmodule
\let\normalusepgflibrary \usepgflibrary
\let\normalusetikzlibrary\usetikzlibrary

\installnamespace{pgfmhack}
\installnamespace{pgflhack}
\installnamespace{tikzhack}

\tolerant\protected\def\usepgfmodule[#1]#;#2% somehow both variants are used
  {\starttikzinput
   \normalusepgfmodule[#1#2]%
   \stoptikzinput}

\tolerant\protected\def\usepgflibrary[#1]#;#2% somehow both variants are used
  {\starttikzinput
   \normalusepgflibrary[#1#2]%
   \stoptikzinput}

\tolerant\protected\def\usetikzlibrary[#1]#;#2% somehow both variants are used
  {\starttikzinput
   \normalusetikzlibrary[#1#2]%
   \stoptikzinput}

\unprotect

\protected\def\installsomethingtikzindeed#1#2#3#4%
  {\defcsname#1:b:#2\endcsname{#3}%
   \defcsname#1:a:#2\endcsname{#4}}

\protected\def\usesomethingtikzindeed#1#2#3%
  {\ifcsname#1:l:#3\endcsname\else
     \letcsname#1:l:#3\endcsname\relax
     \begincsname#1:b:#3\endcsname
     \starttikzinput
     #2[#3]%
     \stoptikzinput
     \begincsname#1:a:#3\endcsname
   \fi}

% somehow both variants [#1] and {#1} are used

\protected\def\installtikzlibraryhacks{\installsomethingtikzindeed\????tikzhack}
\protected\def\installpgfmodulehacks  {\installsomethingtikzindeed\????pgfmhack}
\protected\def\installpgflibraryhacks {\installsomethingtikzindeed\????pgflhack}

\tolerant\protected\def\usepgfmodule  [#1]#;#2{\usesomethingtikzindeed\????pgfmhack\normalusepgfmodule  {#1#2}}
\tolerant\protected\def\usepgflibrary [#1]#;#2{\usesomethingtikzindeed\????pgflhack\normalusepgflibrary {#1#2}}
\tolerant\protected\def\usetikzlibrary[#1]#;#2{\usesomethingtikzindeed\????tikzhack\normalusetikzlibrary{#1#2}}

\protect

\usepgfmodule[shapes]
\usepgfmodule[plot]
\usepgfmodule[matrix]
\usepgfmodule[decorations]

\definefilesynonym [pgfplots]      [tikz-pgfplots]
\definefilesynonym [pgfplotstable] [tikz-pgfplotstable]

% Here come the hacks:

\installtikzlibraryhacks{calendar}{}{\tikzinputfile{pgfcalendar.code.tex}}

\startsetups tikz:hacks:tikzcd
\permanent\protected\def\starttikzcd%
  {\starttikzinput
   \hcontainer\bgroup
   \tikzcd}

\permanent\protected\def\stoptikzcd
  {\endtikzcd
   \egroup
   \stoptikzinput}
\stopsetups

\installtikzlibraryhacks{cd}{}{\directsetup{tikz:hacks:tikzcd}}

\stopmodule

[-- Attachment #3: Type: text/plain, Size: 511 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / https://mailman.ntg.nl/mailman3/lists/ntg-context.ntg.nl
webpage  : https://www.pragma-ade.nl / https://context.aanhet.net (mirror)
archive  : https://github.com/contextgarden/context
wiki     : https://wiki.contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-11-26  6:46 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-11-25 15:07 [NTG-context] Tikz CD Aditya Mahajan
2024-11-25 15:50 ` [NTG-context] " Hans Hagen
2024-11-26  6:39   ` Aditya Mahajan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).