Text Formattingnewcommand for 4x4 matrix

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
centguy
Posts: 18
Joined: Tue Jan 06, 2009 5:42 am

newcommand for 4x4 matrix

Post by centguy »

Hi.

I am happily using

Code: Select all

\newcommand{\mtwo}[4]{{\left( \begin{array}{cc} #1 & #2 \\ #3 & #4 \end{array} \right)} }
\newcommand{\mthree}[9]{
{\left( \begin{array}{ccc} #1 & #2 & #3  \\ #4 & #5 & #6 \\ #7 & #8 & #9 \end{array} \right)} }
to quickly get 2x2 or 3x3 matrices. Just input either 4 numbers
or 9 numbers.

Now, I need to deal with 4x4 matrices quite a lot, but the above tricks don't work
anymore since the maximum #9 is used.

How can I define \mfour that takes in 16 arguments ?

Thanks!
Last edited by cgnieder on Sat Nov 02, 2013 4:46 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

newcommand for 4x4 matrix

Post by localghost »

The 9-argument limit for commands can be evaded [1]. But instead of new commands it might be better to use the matrix environments from amsmath.

Code: Select all

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{mathtools}  % loads »amsmath«

\begin{document}
  \[
    \begin{pmatrix}
       1 &  2 &  3 &  4 \\
       5 &  6 &  7 &  8 \\
       9 & 10 & 11 & 12 \\
      13 & 14 & 15 & 16
    \end{pmatrix}
  \]
\end{document}
The definition of new commands wouldn't make the input shorter here.

[1] UK TeX FAQ – How to break the 9-argument limit


Thorsten
centguy
Posts: 18
Joined: Tue Jan 06, 2009 5:42 am

newcommand for 4x4 matrix

Post by centguy »

Thanks. I think I solved this by
creating a file called `mfour' that contains

Code: Select all

\left( \begin{array}{cccc}
\twobyfourN
{ }{ }{ }{ }
{ }{ }{ }{ }
\twobyfourN
{ }{ }{ }{ }
{ }{ }{ }{ }
\end{array} \right)
where

Code: Select all

\newcommand{\twobytwoN}[4]{ #1 & #2 \\ #3 & #4\\ }

Now, every time I need a 4-by-4 matrix, I use vi to read in mfour,
then just fill in the entries, without even have to type lots of
{} !

I am pleased with this solution. But others might have other solution.

I am now using similar mthree and mtwo to get 3x3 and 2x2 matrices
using the same shortcuts.

Just to share my thoughts.
Last edited by cgnieder on Mon Nov 04, 2013 2:45 pm, edited 1 time in total.
Post Reply