使用指令插入 \\ 與直接插入的效果不同

使用指令插入 \\ 與直接插入的效果不同

我正在使用mathpartir包中,我產生了一些程式碼(由 LyX 產生),這些程式碼將一個環境(我稱之為lines)放入其中,並且每當其中有兩行以上(由 分隔)時\inferrule*,它似乎就會很麻煩。我嘗試了幾件事,但只要環境中存在分隔的兩行,就無法編譯任何內容。pdflatex\\\\lines

以下程式碼正是我在 Pastebin 上傳的程式碼:http://pastebin.com/yBfm1zQ0

\documentclass{article}
\usepackage{mathpartir}
\begin{document}

\newcommand{\testa}{
  This is the intended use:
  \begin{mathpar}
    \inferrule*{
      1\\
      2
    }{
      3
    }
  \end{mathpar}
}

\newcommand{\testb}{
  % LaTeX Error: \begin{mathpar} on input line 68 ended by \end{lines}. [\testb]
  But (because the code is generated), I have an environment in the rule:
  \newenvironment{lines}{}{}
  \begin{mathpar}
    \inferrule*{
      \begin{lines}
        1\\
        2
      \end{lines}
    }{
      3
    }
  \end{mathpar}
}

\newcommand{\testc}{
  If \textbackslash\textbackslash is renamed \textbackslash{}plop, it works (or well, the output isn't what is expected but I know how to fix it and at least it compiles):

  \newcommand{\plop}{\\\relax}
  \newenvironment{lines}{}{}
  \begin{mathpar}
    \inferrule*{
      \begin{lines}
        1\plop
        2
      \end{lines}
    }{
      3
    }
  \end{mathpar}
}

\newcommand{\testd}{
  % LaTeX Error: \begin{mathpar} on input line 70 ended by \end{lines}. [\testd]
  And it's not the \textbackslash{}relax that fixes it:
  \newenvironment{lines}{}{}
  \begin{mathpar}
    \inferrule*{
      \begin{lines}
        1\\\relax
        2
      \end{lines}
    }{
      3
    }
  \end{mathpar}
}

% Line 66
%\testa
\testb
%\testc
%\testd


\end{document}

預先感謝您的幫忙。

答案1

環境是一個群組,所以你的程式碼就像

 {#1\\#2}

這不起作用,您可以定義一個破壞分組的非環境,因此定義更像是

{}#1\\#2{}

有兩個虛假的空組,而不是圍繞整個事物的組。

\documentclass{article}
\usepackage{mathpartir}
\begin{document}

\newcommand{\testa}{
  This is the intended use:
  \begin{mathpar}
    \inferrule*{
      1\\
      2
    }{
      3
    }
  \end{mathpar}
}

\makeatletter
  \newenvironment{lines}{\endgroup}{\begingroup\def\@currenvir{lines}}
\makeatother
\newcommand{\testb}{
  % LaTeX Error: \begin{mathpar} on input line 68 ended by \end{lines}. [\testb]
  But (because the code is generated), I have an environment in the rule:
  \begin{mathpar}
    \inferrule*{
      \begin{lines}
        1\\
        2
      \end{lines}
    }{
      3
    }
  \end{mathpar}
}

\newcommand{\testc}{
  If \textbackslash\textbackslash is renamed \textbackslash{}plop, it works (or well, the output isn't what is expected but I know how to fix it and at least it compiles):

  \newcommand{\plop}{\\\relax}
  \newenvironment{lines}{}{}
  \begin{mathpar}
    \inferrule*{
      \begin{lines}
        1\plop
        2
      \end{lines}
    }{
      3
    }
  \end{mathpar}
}

\newcommand{\testd}{
  % LaTeX Error: \begin{mathpar} on input line 70 ended by \end{lines}. [\testd]
  And it's not the \textbackslash{}relax that fixes it:
  \newenvironment{lines}{}{}
  \begin{mathpar}
    \inferrule*{
      \begin{lines}
        1\\\relax
        2
      \end{lines}
    }{
      3
    }
  \end{mathpar}
}

% Line 66
%\testa
\testb
%\testc
%\testd


\end{document}

答案2

我找到了解決辦法,但我不明白它>_<

\newcommand{\teste}{
  I finally found a fix. Apparently, \textbackslash{}inferrule* doesn't like having several things in its content so if you wrap everything in \{...\}, it works...
  \newenvironment{lines}{}{}
  \begin{mathpar}
    \inferrule*{
      {
        \begin{lines}
          1\\
          2
        \end{lines}
      }
    }{
      3
    }
  \end{mathpar}
}

最奇怪的是,如果兩個 { 互相接觸,} 也互相接觸,就會失敗 >_<

\newcommand{\teste}{
  I finally found a fix. Apparently, \textbackslash{}inferrule* doesn't like having several things in its content so if you wrap everything in \{...\}, it works...
  \newenvironment{lines}{}{}
  \begin{mathpar}
    \inferrule*{{
      \begin{lines}
        1\\
        2
      \end{lines}
    }}{
      3
    }
  \end{mathpar}
}

相關內容