2

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

  • 1
    yes you can do that, what is the question? – David Carlisle Mar 23 '16 at 17:01
  • It's possible but I doubt it to be very effective. – user31729 Mar 23 '16 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 Mar 23 '16 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. – Steven B. Segletes Mar 23 '16 at 17:20
  • @ChristianHupfer Apart from verbatim most can (including align) – David Carlisle Mar 23 '16 at 19:26
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.