# How to convert an output of a matrix into a PNG image?

Using @m_goldberg's code I have created a matrix with the following command:

MatrixForm[{{n, -1 + n, -2 + n, ⋯, 1}, {2 n, 2 n - 1,
2 n - 2, ⋯, n + 1}, {3 n, 3 n - 1,
3 n - 2, ⋯,
2 n + 1},  {⋮, ⋮, ⋮, ⋱, ⋮}, {n^2, -1 + n^2, -2 + n^2,⋯,
HoldForm[(n - 1) n + 1]}},   TableAlignments -> {Center, Center}] // TraditionalForm


If I save the output I get a small, low quality PNG picture.

So, is there a way to convert an output to a graph, or enlarge it? So that to get a better and larger picture of a matrix? I would appreciate any hint or suggestion.

Thank you very much.

An alternative that produces a sharp image is

m = {{n, -1 + n, -2 + n, ⋯, 1},
{2 n, 2 n - 1, 2 n - 2, ⋯, n + 1},
{3 n, 3 n - 1, 3 n - 2, ⋯, 2 n + 1},
{⋮, ⋮, ⋮, ⋱, ⋮},
{n^2, -1 + n^2, -2 + n^2, ⋯, HoldForm[(n - 1) n + 1]}};
Rasterize@Style[MatrixForm[m, TableAlignments -> {Center, Center}] // TraditionalForm), 30]


Then, right-click the image, choose "Save Graphic As...", and store the image in the desired format.

In response to the OP's comment below, columns can be given different colors by

clr = {Red, Orange, Green, Blue, Magenta};
Rasterize@Style[MatrixForm[MapIndexed[Style[#1, clr[[Last@#2]]] &, m, {2}],
TableAlignments -> {Center, Center}] // TraditionalForm, 30]


Replace Last by First to give rows different colors instead. Also, as noted by george2079 in another comment below, the font can be set by replacing 30 by, for instance, {FontFamily -> Times, 30}.

• annoying that Style changes the font isn't it.. fix with {FontFamily -> Times, 30} – george2079 Nov 6 '15 at 15:46
• Thank you very much for your answer. I would like to colour some columns different colours but so far I can only change the colour for the whole matrix. May I ask if there is a way to colour the columns different colours? Thank you again. – johnny09 Nov 26 '15 at 16:03
Export["test.png", MatrixForm[ ..] // TraditionalForm,
ImageSize -> 500, ImageResolution -> 1000]


ImageResize[
Rasterize@(MatrixForm[{{n, -1 + n, -2 + n, ⋯, 1}, {2 n, 2 n - 1,
2 n - 2, ⋯, n + 1}, {3 n, 3 n - 1, 3 n - 2, ⋯,
2 n + 1},  {⋮, ⋮, ⋮, ⋱, ⋮}, {n^2, -1 + n^2, -2 + n^2,⋯, HoldForm[(n - 1) n + 1]}},
TableAlignments -> {Center, Center}] // TraditionalForm), 500]


Edit bbgodfrey's code for having control of resolution and size:

m = {{n, -1 + n, -2 + n, ⋯, 1},
{2 n, 2 n - 1, 2 n - 2, ⋯, n + 1},
{3 n, 3 n - 1, 3 n - 2, ⋯, 2 n + 1},
{⋮, ⋮, ⋮, ⋱, ⋮},
{n^2, -1 + n^2, -2 + n^2, ⋯, HoldForm[(n - 1) n + 1]}};

Rasterize[
Style[MatrixForm[%, TableAlignments -> {Center, Center}] //
TraditionalForm], RasterSize -> 1000, ImageSize -> 500]
Export["filename.png", %];


In addition to the various approaches yielding rasterized images, you can use MaTeX to obtain a vector graphics:

yourTable = MatrixForm[{{n, -1 + n, -2 + n, ⋯, 1}, {2 n, 2 n - 1,
2 n - 2, ⋯, n + 1}, {3 n, 3 n - 1,
3 n - 2, ⋯,
2 n + 1},  {⋮, ⋮, ⋮, ⋱, ⋮}, {n^2, -1 + n^2, -2 + n^2,⋯,
HoldForm[(n - 1) n + 1]}},   TableAlignments -> {Center, Center}] // TraditionalForm;

<< "MaTeX"
MaTeX[TeXForm[yourTable]]
Export["test.pdf",%]


Here is the result of the above code.

Of course you can combine this with bbgodfrey's answer of using colored columns if you include the color package for latex:

SetOptions[MaTeX, "Preamble" -> {"\\usepackage{color}"}];
`