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

Au101
  • 10,278

1 Answers1

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!]

  • 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? – pipsqueaker117 Apr 20 '17 at 05:21
  • @pipsqueaker117 Yes, that's an entirely different question! I'm fairly sure that this question is answered elsewhere on TeX.SX. –  Apr 20 '17 at 05: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 06:12
  • @egreg Yes, you're right, I do although it's rare that I do this at all these days as it just confuses collaborators. You can I have actually discussed this issue before and I know that the spacing is slightly better with \bigl etc but I forget the finer details. –  Apr 20 '17 at 08:33
  • @pipsqueaker117 sorry that was my edit, in your original post you had written "(with the backslashes, I can't figure out how to get those to show up here)" and I misinterpreted it. Apologies for the confusion everyone! Although OP you might want to get your accounts merged, if you had logged back in with your original TeX.SE count you would have been able to rollback my edit instantly (although perhaps still not soon enough?) – Au101 Apr 20 '17 at 13:25
  • @andrew I have not been able to find the answer elsewhere, which is why I posted my own question. The closest one I've managed to find is this one https://tex.stackexchange.com/questions/294103/latex-non-backslash-symbol-aliases but it doesn't actually say how to make the alias. Could you clarify how to do that? – pipsqueaker117 Apr 20 '17 at 13:41
  • 1
    +1 for "a better way to do this is to properly configure your editor". – Name Apr 20 '17 at 13:42
  • @pipsqueaker117 See updated answer –  Apr 20 '17 at 23:53
  • @Andrew Sorry for the confusion - good update =) – Au101 Apr 20 '17 at 23:59
  • 1
    @Au101 No problem. Seemed like a reasonable interpretation:) –  Apr 21 '17 at 00:33
  • @Andrew Hey, I have a question. Trying to use the catcode command gets me an error concerning "undefined control sequences"- you can see the error reproduced here https://www.overleaf.com/9336482mrcrmkshmfxk on overleaf. Do you know if this is something wrong with my environment, or is this one of those nasty consequences you were talking about? – pipsqueaker117 Apr 30 '17 at 20:17
  • @pipsqueaker117 Yes, this is one of the "unintended consequences". The problem is that all subsequent ( and ) will expand to \begin{pmatrix} and \end{pmatrix}, respectively, including those in the macros and packages that you use. This is why it is safer to use the pequation environment that I defined. Failing that, move the four lines starting with \let\lparen=(\let\rparen=) to after \begin{document}. This fixes your overleaf example but it is unlikely to fix all of the "unintended consequences". –  Apr 30 '17 at 23:28