1

If have the following ConTeXt macros:

\def\tableHeader#1 {%
    \start
    \setupTABLE[frame=on,offset=1mm]
    \setupTABLE[c][1][width=0.25\textwidth]
    \setupTABLE[c][2][width=0.75\textwidth]
    \bTABLE
        \bTR
            \bTH[nc=2] #1 \eTH
        \eTR
}

\def\tableFooter {%
    \eTABLE
    \stop
}

\def\tableItem#1#2 {%
    \bTR
        \bTD #1 \eTD
        \bTD #2 \eTD
    \eTR
}

How do I call this macro with '[\w ]+' as second parameter? That is:

\tableHeader{Example}
\tableItem{Regex}{[\w ]+}
\tableFooter
2
  • 1
    I don't know much about context but I guess the space after #2 in the definition might be a problem as well as using \w which looks like a macro but your input suggests it should be verbatim? – cgnieder Jun 12 '17 at 13:52
  • Yes, it should be verbatim. The second parameter is a regex pattern (including the space). – Martijn Rijkeboer Jun 12 '17 at 13:55
3

Here you want to ensure that [ isn't seen as the start of an argument by \bTD. To do that, the easiest way is to insert a \relax. As you are making this part of a macro, I'd do that in the definition not each time you pass an argument

\def\tableItem#1#2 {%
    \bTR
        \bTD\relax #1 \eTD
        \bTD\relax #2 \eTD
    \eTR
}
\starttext
\bTABLE
\tableItem{Regex}{[\string\w\space]+}
\eTABLE
\stoptext

I suspect you want something like

\tableItem{Regex}{\type{[\w ]+}}

which would avoid the need for the \relax in this case as \type will also stop the scanning for a [, but it's likely best to keep the \relax anyway.


Working example using edited question: notice that \w has to be made into a string one way or another

\def\tableHeader#1 {%
    \start
    \setupTABLE[frame=on,offset=1mm]
    \setupTABLE[c][1][width=0.25\textwidth]
    \setupTABLE[c][2][width=0.75\textwidth]
    \bTABLE
        \bTR
            \bTH[nc=2] #1 \eTH
        \eTR
}

\def\tableFooter {%
    \eTABLE
    \stop
}

\def\tableItem#1#2 {%
    \bTR
        \bTD\relax #1 \eTD
        \bTD\relax #2 \eTD
    \eTR
}
\starttext
\tableHeader{Example}
\tableItem{Regex}{[\string\w\space]+}
\tableFooter
\stoptext
9
  • For me it doesn't work when I don't use the \type. Unfortunately the \type also makes the text mono-spaced. – Martijn Rijkeboer Jun 12 '17 at 14:19
  • @MartijnRijkeboer I though you might want \type as this looks to me like a situation where you'd expect mono-spaced: it was just a suggestion. However, the example without \type should work: certainly it does for me exactly as written with ConTeXt from TeX Live 2017. Can you elaborate (here or in the question) as to what 'doesn't work'? – Joseph Wright Jun 12 '17 at 14:23
  • I've added \relax to \tableItem as recommended. When I try to use \tableItem{Regex}{[\w ]+} I get l.9 \tableFooter <empty file> ?. I'm using ConTeXt from TeX Live 2016. – Martijn Rijkeboer Jun 12 '17 at 14:40
  • @MartijnRijkeboer Please make a full example and edit it into the question: I'm afraid I'll need more detail to help further. (For example, \tableFooter isn't a standard ConTeXt macro.) – Joseph Wright Jun 12 '17 at 14:43
  • I've updated the question, is this what you meant? I've let the \relax out, the keep the flow of the question and answer. – Martijn Rijkeboer Jun 12 '17 at 15:00
0

If you really to simply type [\w ]+, you can change the catcode table so that \ is not a special character.

One way to do this is to use the \tbacatcodes catcode table (defined in catc-ctx.mkiv), that only sets the normal catcodes of { and }; everything else, including \ has catcode other. Note that this means that you cannot use unmatched { and } inside the argument of \tableItem.

While I was at it, I also made a few stylistic changes to your code:

  1. Use setups to setup table style
  2. Use \define instead of \def. Keep in mind that space is significant in TeX, so \def\tableItem#1#2 { is different from \def\tableItem#1#2{.

The complete code is below

\startsetups regex-table
  \setupTABLE[frame=on,offset=1mm]
  \setupTABLE[c][1][width=0.25\textwidth]
  \setupTABLE[c][2][width=0.75\textwidth]
\stopsetups

\define[1]\tableHeader
  {\bTABLE[setups=regex-table]
    \bTR 
      \bTH[nc=2] #1 \eTH 
    \eTR}

\define\tableFooter{\eTABLE}

\define\tableItem
   {\pushcatcodetable
    \setcatcodetable\tpacatcodes
    \doTableItem}

\define[2]\doTableItem
   {\bTR
        \bTD\relax #1 \eTD
        \bTD\relax #2 \eTD
    \eTR
    \popcatcodetable}

\starttext
\tableHeader{Example}
\tableItem{Regex}{[\w ]+}
\tableFooter
\stoptext
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.