7

I have grid-formatted data:

1 2 3
4 5 6 
7 7 9

I'd like to display this as above in a LaTeX document compiled with pdfLaTeX.

I've thought about using the TikZ Matrix library and the AMSMath Matrix library, but, in the resulting document, highlighting the data (as printed by AMSMath) and copy-pasting it into a notepad shows the following:

1
4
7
2
5
7
1
3
6
9

I'd like a nice grid-formatted result when I hit paste - how can I compile my PDF to produce such a result?

8

You suggested the answer yourself: A TikZ matrix will do just that (which is pretty awesome!):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix[matrix of nodes]{
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9\\% <- Without this %, there would be a trailing "1" in the copied text
};
\end{tikzpicture}
\end{document}

gives

and copying and pasting from the PDF gives

1 2 3
4 5 6
7 8 9

Or, to draw the grid itself, along with the data (as described at TikZ matrix as a replacement for tabular, for example):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix [matrix of nodes, style={nodes={rectangle,draw,minimum width=3em}}, minimum height=1.5em, row sep=-\pgflinewidth, column sep=-\pgflinewidth]
{
1 & 2 & 3\\
4 & 5 & 6\\
7 & 8 & 9\\% <- Without this %, there would be a trailing "1" in the copied text
};
\end{tikzpicture}
\end{document}
  • Jake, I had another piece of code in which I had put boxes around each number (to make a grid) - in that code the copy didn't come out right. Would you know how to do this off-handedly? – Richard Apr 5 '12 at 20:53
  • 1
    Nevermind, I figured it out - odd it didn't work the first time! – Richard Apr 5 '12 at 20:58

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.