2

I want to define an environment that uses two optional arguments, but i want to be able to use one with <> and the other with []

Here is what I have done:

\NewDocumentCommand\why{O{\equiv}m}%
    { #1 & \quad\langle\;\text{{#2}}\;\rangle }

\NewDocumentCommand\res{m}%
    { & #1}

\NewDocumentCommand\To{}{\Rightarrow}

\NewDocumentEnvironment{derivation}{D<>{1.2} O{0pt}}{
  \begingroup
  \renewcommand{\arraystretch}{#1}
  \setlength{\tabcolsep}{#2}
  \begin{tabular}{>{$}l<{$} >{$}l<{$}}
}{
  \end{tabular}
  \endgroup
}

When I try to use any of the optional arguments either they print in the pdf or I just get an error. How can I fix this?

The code of the example is:

\documentclass{article}
\usepackage{xparse}
\usepackage{mathtools}
\usepackage{nicematrix}
\usepackage[spanish]{babel}

\ExplSyntaxOn
\NewDocumentCommand\why{O{\equiv}m}%
    { #1 & \quad\langle\;\text{{#2}}\;\rangle }

\NewDocumentCommand\res{m}%
    { & #1}

\NewDocumentCommand\To{}{\Rightarrow}

\NewDocumentEnvironment{derivation}{D<>{1.2} O{0pt}}{
  \renewcommand{\arraystretch}{#1}
  \setlength{\tabcolsep}{#2}
  \begin{tabular}{>{$}l<{$} >{$}l<{$}}
}{
  \end{tabular}
}
\ExplSyntaxOff

\begin{document}
\begin{center}
  \begin{derivation}<1.5>
          \res{ (p \To \neg q) \land (\neg q \To \neg p) }\\
      \why[\To]{ Transitividad($\To$) }\\
          \res{ p \To \neg p }\\
      \why{ Def.(alt)($\To$) }\\
          \res{ \neg p \lor \neg p }\\
      \why{ Idempotencia($\lor$) }\\
          \res{ \neg p }
  \end{derivation}
\end{center}
\end{document}

enter image description here

Apparently, the issue is the babel package.

7
  • 2
    I don't know enough to help you immediately, but: (1) \begingroup/\endgroup already comes with an environment, I don't think you need it. More importantly, (2) if the arguments are optional, what should happen if they're not given? Can you show us how you're trying to use them, so that they print in the pdf or give you an error? Commented Feb 19, 2023 at 15:16
  • Ignore (2), I was forgetting that syntax. (3) I can't get \begin{tabular}{>{$}l<{$} >{$}l<{$}} to work, even without a new environment. What are you trying to accomplish with that part? Commented Feb 20, 2023 at 2:43
  • 1
    The environment work almost the same as align. The columns in the tabular environment avoid writing $ in every line of the table. Commented Feb 20, 2023 at 4:02
  • 1
    @DavidGómez That's just an array though ... Commented Feb 20, 2023 at 6:49
  • 1
    // When I use the method in tables - Math mode in tabular without having to use $...$ everywhere - TeX - LaTeX Stack Exchange it works well for me, although I got other errors e.g. \res is not defined. // so please write a minimal working example (See link for more details what it means) if you still need help. Commented Feb 20, 2023 at 8:55

1 Answer 1

5

By default, babel-spanish activates < and > for quotes.

This activation is done at begin document, but your code requires LaTeX to look for “ordinary” < and >, which it cannot find.

Solution: pass the es-noquoting option.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[spanish,es-noquoting]{babel}
\usepackage{mathtools}
\usepackage{nicematrix} % loads the package 'array', used in the code

\ExplSyntaxOn
\NewDocumentCommand\why{O{\equiv}m}
    { #1 & \quad\langle\;\text{{#2}}\;\rangle }

\NewDocumentCommand\res{m}
    { & #1}

\NewDocumentCommand\To{}{\Rightarrow}

\NewDocumentEnvironment{derivation}{D<>{1.2} O{0pt}}{%
  \renewcommand{\arraystretch}{#1}%
  \setlength{\tabcolsep}{#2}%
  \begin{tabular}{>{$}l<{$} >{$}l<{$}}%
}{
  \end{tabular}%
}
\ExplSyntaxOff

\begin{document}

\begin{center}
  \begin{derivation}<1.5>
          \res{ (p \To \neg q) \land (\neg q \To \neg p) }\\
      \why[\To]{ Transitividad($\To$) }\\
          \res{ p \To \neg p }\\
      \why{ Def.(alt)($\To$) }\\
          \res{ \neg p \lor \neg p }\\
      \why{ Idempotencia($\lor$) }\\
          \res{ \neg p }
  \end{derivation}
\end{center}
\end{document}

I removed the call to xparse that's not needed since October 2020. Instead I added fontenc with the T1 option, which is needed for Spanish.

Check the positions of %.

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.