具有小型頁面的自訂環境會導致「\end 定義中的參數編號非法」

具有小型頁面的自訂環境會導致「\end 定義中的參數編號非法」

在 Windows 上將 MiKTeX 與 PDFLaTeX 結合使用

我想創建一個包含兩個小型頁面的環境;第一個有一個描述項,第二個有一個小文字(或圖片)。這段程式碼按照我想要的方式運作:

{
\noindent
\begin{minipage}{0.5\textwidth}
\begin{description}
    \item[Label] Description
\end{description}
\end{minipage}\hfill
\begin{minipage}{0.3\textwidth}
    Second minipage
\end{minipage}
}

我想創建一個巨集或環境,讓我可以輕鬆地引入此類內容。因此,我使用兩個參數來建立以下環境:

\newenvironment{myenvironment}[2]% 1:label, 2:second minipage text
{%
    \noindent
    \begin{minipage}{0.5\textwidth}
    \begin{description}
        \item[#1] % Description
}%
{%
    \end{description}
    \end{minipage}\hfill
    \begin{minipage}{0.3\textwidth}
        #2
    \end{minipage}
}

但是當我嘗試使用它時

\begin{myenvironment}{Label}{Second minipage text}
Description text
\end{myenvironment}

Illegal parameter number in definition of \endmyenvironment.指向完成聲明的行newenvironment。經過幾個小時的努力後,我嘗試使用雙散列(##)並得到兩個新的不同錯誤:You can't use `macro parameter character #' in restricted horizontal modeYou can't use `macro parameter character #' in internal vertical mode。這次錯誤指向我想使用環境時的行,但對定義沒有抱怨。

我究竟做錯了什麼?

答案1

與我的問題無關##。看起來您不能在自訂環境的結束或結束程式碼中使用參數。有解決方法,但在我看來這應該是一定有特徵。使用可能的解決方法——輔助變數——,我到達了這個工作環境:

\newenvironment{myenvironment}[2]%
{%
    \def\myenvargumentII{#2}    
    \noindent
    \begin{minipage}{0.5\textwidth}
    \begin{description}
        \item[#1] % Description
}%
{%
    \end{description}
    \end{minipage}\hfill
    \begin{minipage}{0.3\textwidth}
        \myenvargumentII
    \end{minipage}
}

這個解決方案來得太晚了。同時,我設法用帶有 3 個參數的\newcommanda解決了我的問題:

\newcommand{\myminipages}[3]% 1:label, 2:secondminipage, 3:Description
{%
    \noindent
    \begin{minipage}{0.5\textwidth}
        \begin{description}
            \item[#1] #3
        \end{description}
    \end{minipage}\hfill
    \begin{minipage}{0.3\textwidth}
        #2
    \end{minipage}
    \medskip % vertical space
}

可與以下產品一起使用:

\myminipages{Label}{Second minipage text}{Description text}

希望能幫助你。


newcommand[a] 看起來該方法相對於該方法沒有特別的優勢newenvironment,至少對於這種用途而言。更多的這裡這裡

相關內容