0

I want to pass an argument to a self-made tabular environment in the following format:

\begin{customTabular}{|c|c|}{'@'}

...such that the second argument (here '@', WITHIN PARANTHESIS) is treated as a column separator symbol by the LaTeX document. How this can be done? Please suggest the changes/modifications in the code attached.

The code for customTabular environment is as follows (here, mytabular is similar to customTabular):

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentEnvironment {mytabular} { O{\,} }
  {
    \char_set_catcode_other:N \&
    \char_set_catcode_alignment:n {`#1}
    \begin{tabular}
  }
  {
    \end{tabular}
  }
\ExplSyntaxOff
1
  • 1
    Is there any particular reason for wanting the quotes?
    – egreg
    Mar 25, 2019 at 22:21

2 Answers 2

2

You need to make the argument an optional group (or perhaps mandatory), and to arrange to strip any quote marks before using as a number:

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentEnvironment {mytabular} { O{c} m G{','} }
  {
    \char_set_catcode_other:N &
    \char_set_catcode_alignment:n { \__mytab_test:n {#3} }
    \begin{tabular}[#1]{#2}
  }
  {
    \end{tabular}
  }
\cs_new:Npn \__mytab_test:n #1 { \__mytab_test:w #1 ' #1 '' \q_stop }
\cs_new:Npn \__mytab_test:w #1 ' #2 ' #3 \q_stop { `#2 }
\ExplSyntaxOff

\begin{document}
\begin{mytabular}{cc}{'@'}
  a @ b\\
  c @ d
\end{mytabular}

\begin{mytabular}{cc}{@}
  a @ b\\
  c @ d
\end{mytabular}
\end{document}

The trick in removing ' tokens is that TeX is greedy in a match. So with

#1 ' #2 ' #3

any input containing a leading ' will have an empty #1, the 'argument' in #2 and rubbish left in #3. If there was no leading ', then #1 will be the same as #2 due to the set up macro, which supplies the input both with and without additional ' tokens.

1
  • Thank You @Joseph. The answer really worked, but the syntax used is quite complicated and out of my current understanding of LaTeX. But, still the answer was helpful and it worked as the way I required. Please provide a simpler solution to the problem if possible.Thank You for solving my question :)
    – Naman
    Mar 25, 2019 at 12:34
0

If you don't require the quotes, your code works, provided you use square brackets for the argument. It's better style to also add the mandatory argument for the columns.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentEnvironment {mytabular} { O{\,} m }
  {
    \char_set_catcode_other:N \&
    \char_set_catcode_alignment:n {`#1}
    \begin{tabular}{#2}
  }
  {
    \end{tabular}
  }
\ExplSyntaxOff

\begin{document}

\begin{mytabular}[@]{cc}
  a @ b\\
  c @ d
\end{mytabular}

\begin{mytabular}{cc}
  a , b\\
  c , d
\end{mytabular}

\end{document}

enter image description here

If you want a syntax such as \begin{mytabular}{cc}[@], just exchange the arguments

\NewDocumentEnvironment {mytabular} { m O{\,} }
  {
    \char_set_catcode_other:N \&
    \char_set_catcode_alignment:n {`#2}
    \begin{tabular}{#1}
  }
  {
    \end{tabular}
  }
3
  • Thank You for your contribution but I do want the use of quotes in the argument passing method in the environment.
    – Naman
    Mar 26, 2019 at 12:02
  • @Naman What are they for?
    – egreg
    Mar 26, 2019 at 12:06
  • Nothing much, rather, only a new way of trying the old stuff...
    – Naman
    Mar 26, 2019 at 16:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .