Tikz code not working in LyX but working in pure Tex

I'm trying to add a TikZ picture in my LyX document. To do this, I organize the TikZ tex code in Texmaker (a friend sent me a tikz code model in a .tex file) and then I copy it to a ERT in LyX (making sure to use the correct packages). The problem is that the code works in Texmaker when I export it to pdf, but doesn't work when I try to preview it with LyX! I get the error:

File ended while scanning use of \pgfkeys@@qset.

<*>newfile3.tex

*** (job aborted, no legal \end found)


The tex code that is working in Texmaker is the following:

\documentclass[12pt,a4paper]{article}

\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\tikzset{
% Two node styles for game trees: solid and hollow
solid node/.style={circle,draw,inner sep=1.5,fill=black},
hollow node/.style={circle,draw,inner sep=1.5}
}

\begin{center}
\begin{tikzpicture}[scale=1.5,font=\footnotesize]
% Specify spacing for each level of the tree
\tikzstyle{level 1}=[level distance=15mm,sibling distance=35mm]
\tikzstyle{level 2}=[level distance=15mm,sibling distance=15mm]
\tikzstyle{level 3}=[level distance=15mm,sibling distance=8mm]
% The Tree
\node(0)[hollow node,label=above:{Jogador 1},label=right:{$x''$}]{}
child{node(1)[solid node]{}
child{node(3)[solid node,label=right:{$x$}]{}
child{node[solid node,label=below:{$(a_1,b_1)$}]{} edge from parent node[left]{$C$}}
child{node[solid node,label=below:{$(a_2,b_2)$}]{} edge from parent node[right]{$D$}}
edge from parent node[left]{$L$}}
child{node(4)[solid node]{}
child{node[solid node,label=below:{$(a_3,b_3)$}]{} edge from parent node[left]{$C$}}
child{node[solid node,label=below:{$(a_4,b_4)$}]{} edge from parent node[right]{$D$}}
edge from parent node[left]{$R$}}edge from parent node[left,xshift=-3]{$A$}
}
child{node(2)[solid node]{}
child{node(5)[solid node,label=right:{$x'$}]{}
child{node[solid node,label=below:{$(a_5,b_5)$}]{} edge from parent node[left]{$C$}}
child{node[solid node,label=below:{$(a_6,b_6)$}]{} edge from parent node[right]{$D$}}
edge from parent node[left]{$L$}}
child{node(6)[solid node]{}
child{node[solid node,label=below:{$(a_7,b_7)$}]{} edge from parent node[left]{$C$}}
child{node[solid node,label=below:{$(a_8,b_8)$}]{} edge from parent node[right]{$D$}}
edge from parent node[right]{$R$}
}edge from parent node[right,xshift=3]{$B$}
};
% information set
\draw[dashed,rounded corners=10]($(1) + (-.2,.25)$)rectangle($(2) +(.2,-.25)$);
% specify mover at 2nd information set
\node at ($(1)!.5!(2)$) {Jogador 2};
\draw[dashed, rounded corners = 10]($(3) + (-.2,.25)$)rectangle($(6) +(.2,-.25)$);
\node at ($(4)!.5!(5)$) {Jogador 1};
\end{tikzpicture}
\end{center}

\end{document}


So I open LyX, copy all the code between \begin{document} and \end{document}, and put the following in the document preamble:

\usepackage{tikz}
\usetikzlibrary{calc}


But when I try to preview/export to pdf, I get the error I copied above. Can anyone help me, please? Thanks!

-
The error usually pops up when a closing bracket of the options or the brace of some style is missing. –  percusse Jul 17 '14 at 8:16
percusse, but why does it work in Texmaker but not in LyX? –  LuizMMB Jul 17 '14 at 14:44
After you make your LyX file, export it (File > Export) to see what the LaTeX is that is being run. Likely there is something different in the preamble (LyX adds stuff to the preamble also) –  scottkosty Jul 17 '14 at 23:49

When copy-pasting stuff into ERTs, line breaks are not preserved. As a result of this, everything after % Two node styles .. is not seen by TeX, all it sees before \end{document} is \tikzstyle{, causing the error. To paste text preserving line breaks, use Edit --> Paste special --> Plain text (shortcut Ctrl + Shift + V), see this old bug report: http://www.lyx.org/trac/ticket/6821
Another way is to save the code snippet in a new .tex file, and insert it with Insert --> File --> Child document, setting Input as Include type.