2

How can I define a command macro using \newcommand so that the argument to the command is surrounded by an environment?

8
  • 1
    yes you can do that, what is the question? Commented Mar 23, 2016 at 17:01
  • It's possible but I doubt it to be very effective.
    – user31729
    Commented Mar 23, 2016 at 17:05
  • @David Ah my mistake, the code in my question wasn't working on my machine, but it was for an unrelated reason.
    – agcha
    Commented Mar 23, 2016 at 17:16
  • 1
    One should note that not all environments can be macro-ized in this way. Sometimes, \newcommand\mat[1]{\envname #1\endenvname} must be used. Sometimes, not even that works. Commented Mar 23, 2016 at 17:20
  • @ChristianHupfer Apart from verbatim most can (including align) Commented Mar 23, 2016 at 19:26

1 Answer 1

5

A proof that it is possible and using an optional argument that can be b or p or anything of the known prefixes (e.g. v, B and V)

Besides that all: It does not improve readability of the code, in my point of view.

\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}

enter image description here

You must log in to answer this question.

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