0

I'm wondering if there's any way that I can alias the ( and ) buttons to \begin{pmatrix} and \end{pmatrix}, so that when I type those characters in math mode, LaTeX will magically replace them with the correct commands.

As far as I can tell there's no reason to use the "dumb" versions, and it would save me a whole lot of time. Is this possible?

I'm not actually asking how to redefine (. I'm asking LaTeX to replace all (s in math mode with \begin{pmatrix}.

3
  • 5
    All that time you have trimmed by these shortcuts, you will loose at least twice in the first error debugging.
    – percusse
    Apr 20 '17 at 2:56
  • Or create a new environment based on matrix, \left and \right. Apr 20 '17 at 3:26
  • 2
    are you really never going to use parentheses in any way other than specifying matrices? i find that rather unlikely, even limiting it to math. not a good idea. Apr 20 '17 at 14:38
4

Edit

The original question that I saw asked for \( ... \) to expand to \begin{pmatrix} ... \end{pmatrix} but, in fact, the OP wants ( ... ) to automatically expand to \begin{pmatrix} ... \end{pmatrix}. I voted to close this because it is a duplicate of Automatic left and right commands (see the answer of LSpice), and then the question was closed and reopened. So, I re-answer.

The trick to doing what the OP wants is to make ( and ) active characters. Once this is done, they can be redefined at will:

\documentclass{amsart}

\let\lparen=(\let\rparen=)           % save parentheses
\catcode`(=\active\catcode`)=\active % make them active
\def({\begin{pmatrix}}               % redefine them
\def){\end{pmatrix}}

\begin{document}

  \[    ( 1 & 0  & 1 \\ 0 & 1 & 0\\ 0 & 0 & 1 ) \]

\end{document}

With this code, if you want to type ( or ) then you need to use \lparen and \rparen. It is quite likely that this will have weird and not so wonderful side effects.

A slightly better approach would be to define an environment where ( and ) expand to give a pmatrix and outside of this they behave normally. Redefining catcodes inside an environment requires a little more gymnastics (see How could one change the catcodes inside the definition of an environment?):

\documentclass{amsart}

\let\lparen=(\let\rparen=)% save parentheses

\catcode`(=\active\catcode`)=\active% change catcode for newenvironment
\newenvironment{pequation}{%
  \catcode`(=\active\catcode`)=\active% make them active in environment
  \def({\begin{pmatrix}}% redefine them
  \def){\end{pmatrix}}%
  \equation%
}{\endequation}
\catcode`(=12\catcode`)=12% restore to original catcodes

\begin{document}

  \begin{pequation}
   ( 1 & 0  & 1 \\ 0 & 1 & 0\\ 0 & 0 & 1 )
  \end{pequation}

  $\sin(x)$
\end{document}

Original answer

You can do this if you really want to but, as the comments show, most people would recommend against doing this. The main issue is that, by default, LaTeX uses \( ... \) to typeset mathematics, so it is (almost) equivalent to $ ... $ --- in fact, by Are \( and \) preferable to dollar signs for math mode?, \(...\) provides better error messages. The \(...\) command is a counterpoint to the way LaTeX encourages us to typeset displayed equations using \[ ... \]. Note that \[ ... \] is not equivalent to $$ ... $$! See Why is \[ ... \] preferable to $$ ... $$?.

This said, you can override the definition of any command with \renewcommand (or \def). Overriding the definition of a command should be done with care because doing this might break something else.

These caveats aside, the following does what you want:

\documentclass{amsart}

\renewcommand\({\begin{pmatrix}}
\renewcommand\){\end{pmatrix}}

\begin{document}

Here is a wonderful matrix
\[    \( 1 & 0 & 1 \\ 0 & 1 & 0\\ 0 & 0 & 1\) \]

\end{document}

There may be unintended consequences but I think that this should be OK, although it may well confuse your coauthors! In my opinion, a better way to do this is to properly configure your editor:)

[I must confess that I used to redefine \( to be \bigl(, and \) to \bigr), so I am on dubious grounds with the moral statements above that you shouldn't do this!]

12
  • Hi, OP here- for some reason, backslashes got inserted before the parentheses where I didn't mean them to. So I'm not actually asking how to redefine "backslash (", it's more that I'm asking latex to replace all "("s in math mode with "\begin{pmatrix}". Does that change your answer? Apr 20 '17 at 5:21
  • @pipsqueaker117 Yes, that's an entirely different question! I'm fairly sure that this question is answered elsewhere on TeX.SX.
    – user30471
    Apr 20 '17 at 5:40
  • 1
    I hope that, at least, you define \( and \) to be \bigl( and \bigr), because \big is really wrong.
    – egreg
    Apr 20 '17 at 6:12
  • 1
    +1 for "a better way to do this is to properly configure your editor".
    – Name
    Apr 20 '17 at 13:42
  • 1
    @Au101 No problem. Seemed like a reasonable interpretation:)
    – user30471
    Apr 21 '17 at 0:33

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.