環境的命令宏

環境的命令宏

如何使用定義命令宏,\newcommand以便命令的參數被環境包圍?

答案1

證明這是可能的,並使用可選參數,該參數可以是bp或任何已知前綴(例如vBV

除此之外:在我看來,它並沒有提高程式碼的可讀性。

\documentclass{article}

\usepackage{amsmath}

\newcommand{\mat}[2][b]{%
  \begin{#1matrix}
    #2
  \end{#1matrix}
}

\begin{document}
$\mat{ 1 & 2 \\ 3 & 4 \\}$ and $ \mat[p]{ 1 & 2 \\ 3 & 4 \\}$ and $\mat[B]{ 1 & 2 \\ 3 & 4 \\}$ and $\mat[v]{ 1 & 2 \\ 3 & 4 \\}$ and $ \mat[V]{ 1 & 2 \\ 3 & 4 \\}$
\end{document}

在此輸入影像描述

相關內容