1

I'm trying to format a confusion matrix in lyx. I want the data to look like this.

    a    b    c    d    e    f    g   <-- classified as
    1    5   10    8    4    2    0 |    a = 3
    3   39  100   66    6    2    0 |    b = 4
    4   74 1385  596   69   10    0 |    c = 5
   10   47  631 1763  346   39    0 |    d = 6
    1   11   80  417  533   37    0 |    e = 7
    1    0   11   50   62   69    0 |    f = 8
    0    0    0    1    4    0    0 |    g = 9

Notice how it is structured properly in the sense that all values align to a column and row.

When I paste this in lyx it looks like below and I don't seem to have a way to add spaces or tabs.

enter image description here

I'm using the default Article class.

2 Answers 2

3

LyX can handle direct copy-paste of ASCII tables into lyx tables. You just have the columns delimited by tabs in the ASCII table. Then copy the ASCII table from your ASCII editor to the clipboard. In LyX, create an empty table with the appropriate number of columns and rows, e.g. by the menu Insert->Table or even quicker with the pulldown menu in the toolbar. Then place the cursor in the upper left cell of this empty table and do Ctrl-Shift-V. This is much faster than retyping the table as LaTeX markup.

1

I don't know how to input it in LyX, but this LaTeX code should do.

\documentclass{article}
\begin{document}

\begin{tabular}{ *{7}{r} | c@{}l }
\multicolumn{1}{c}{a} &
\multicolumn{1}{c}{b} &
\multicolumn{1}{c}{c} &
\multicolumn{1}{c}{d} &
\multicolumn{1}{c}{e} &
\multicolumn{1}{c}{f} &
\multicolumn{1}{c}{g} &
\multicolumn{2}{l}{$\leftarrow$ classified as} \\[\jot]
    1 &   5 &  10 &   8 &   4 &   2 &   0 & a & ${}=3$ \\
    3 &  39 & 100 &  66 &   6 &   2 &   0 & b & ${}=4$ \\
    4 &  74 &1385 & 596 &  69 &  10 &   0 & c & ${}=5$ \\
   10 &  47 & 631 &1763 & 346 &  39 &   0 & d & ${}=6$ \\
    1 &  11 &  80 & 417 & 533 &  37 &   0 & e & ${}=7$ \\
    1 &   0 &  11 &  50 &  62 &  69 &   0 & f & ${}=8$ \\
    0 &   0 &   0 &   1 &   4 &   0 &   0 & g & ${}=9$ \\
\end{tabular}
\end{document}

enter image description here

If you want all columns with equal width, then siunitx seems to provide the easiest way.

\documentclass{article}
\usepackage{siunitx}
\begin{document}

\begin{tabular}{ *{7}{S[table-format=4.0]} | c@{}l }
\multicolumn{1}{c}{a} &
\multicolumn{1}{c}{b} &
\multicolumn{1}{c}{c} &
\multicolumn{1}{c}{d} &
\multicolumn{1}{c}{e} &
\multicolumn{1}{c}{f} &
\multicolumn{1}{c}{g} &
\multicolumn{2}{l}{$\leftarrow$ classified as} \\[\jot]
    1 &   5 &  10 &   8 &   4 &   2 &   0 & a & ${}=3$ \\
    3 &  39 & 100 &  66 &   6 &   2 &   0 & b & ${}=4$ \\
    4 &  74 &1385 & 596 &  69 &  10 &   0 & c & ${}=5$ \\
   10 &  47 & 631 &1763 & 346 &  39 &   0 & d & ${}=6$ \\
    1 &  11 &  80 & 417 & 533 &  37 &   0 & e & ${}=7$ \\
    1 &   0 &  11 &  50 &  62 &  69 &   0 & f & ${}=8$ \\
    0 &   0 &   0 &   1 &   4 &   0 &   0 & g & ${}=9$ \\
\end{tabular}
\end{document}

enter image description here

3
  • what are those little "ghost" lines running across row "e"? i'd suspect my eyes or my monitor, except that they (the lines, not the eyes) persist when i scroll up to enlarge the image, and it's consistent on both examples. Jan 11, 2015 at 14:51
  • @barbarabeeton Artifacts when getting the image as a screen shot. I polished them.
    – egreg
    Jan 11, 2015 at 14:59
  • You could past the above latex code, or simply construct the table directly in LyX.
    – Nigel
    Jan 13, 2015 at 9:44

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.