# Cannot create confusion matrix in lyx

I'm trying to create a confusion matrix using LyX, I've found this code to create a confusion matrix so inside my document I pressed Ctrl+l where I wanted the matrix and I pasted the code but I obtain this error: LaTeX error: Can be used only in preamble. The line with the error is the first one: \begin{document}.

What should I paste to obtain only a confusion matrix? I suppose that what I pasted was the full code of a latex page.

• Welcome to TeX.SX! The usepackages have to be added to your preamble (and the command too if you like). Then the part around the tabular environment is what you insert into the document part. – TeXnician Jul 4 '18 at 17:03

## 1 Answer

Assuming the version with the rotated text. In Document -> Settings -> LaTeX preamble, add

\usepackage{array}
\usepackage{graphicx}
\usepackage{multirow}

\newcommand\MyBox[2]{
\fbox{\lower0.75cm
\vbox to 1.7cm{\vfil
\hbox to 1.7cm{\hfil\parbox{1.4cm}{#1\\#2}\hfil}
\vfil}%
}%
}


In the ERT:

\noindent
\renewcommand\arraystretch{1.5}
\setlength\tabcolsep{0pt}
\begin{tabular}{c >{\bfseries}r @{\hspace{0.7em}}c @{\hspace{0.4em}}c @{\hspace{0.7em}}l}
\multirow{10}{*}{\rotatebox{90}{\parbox{1.1cm}{\bfseries\centering actual\\ value}}} &
& \multicolumn{2}{c}{\bfseries Prediction outcome} & \\
& & \bfseries p & \bfseries n & \bfseries total \\
& p$'$ & \MyBox{True}{Positive} & \MyBox{False}{Negative} & P$'$ \\[2.4em]
& n$'$ & \MyBox{False}{Positive} & \MyBox{True}{Negative} & N$'$ \\
& total & P & N &
\end{tabular}


Note that when you paste code into ERTs, use Ctrl + Shift + V (Edit -> Paste special), otherwise line breaks in the code aren't preserved.

In general for such examples, things that are between \documentclass and \begin{document} go in the preamble. The part between (but not including) \begin{document} and \end{document} go in ERTs.

• What I obtained is something magic! I didn't know neither the paste special nor the difference between text and preamble. Really, really, thank you for the answer and the patience! – Timmy Jul 5 '18 at 8:17