# Inserting evil red code in LyX in mathmode

How can I put code inside a math environment in LyX. For example, I want to insert a matrix, that is created via the \tikzpicture command, like in the first answer from here. But if I just copy the code from there and paste it into math mode, it turns to text - I don't seems to be able to insert evil red code in math mode. I can only type (which is tiring) some of the code hand, but can't insert text like \\ this automatically is converted from evil red code to a text backslash.

Is there any solution for this ? I would really like to have LyX display to me the matrices on the left and right, from the post of the link above, and have only the middle in evil red code.

(Of course I could just put everything in evil red code, including the math stuff, but then the left and right matrices would also be in code, which would be a pity).

-
write the complete code with ERT –  Herbert Jun 20 '12 at 10:50
I really hate that LyX refers to code as "Evil". It may be jokey, but it's alienating LyX users from the power of TeX. –  Seamus Jun 20 '12 at 12:04
@Herbert Yes, as I said, in the last paragraph, that would be an option...but an ugly one. –  user10324 Jul 14 '12 at 14:10
To everybody else who reads this (so far unanswered) question: A negative answer is also a solution (at least then I could stop looking for one!) –  user10324 Jul 14 '12 at 14:11

[Something of an extended comment]

Lyx can handle a variety of math mode items, but where it cannot handle them you have to insert raw LaTeX code ('ERT'). Other than extending the Lyx binary, there is not much that can be done here. (Even then, there will still be limits to what Lyx can handle.)

-

I have just finished writing a document where I pasted in matrices, integrals,etc using tex code (ERC). It exported fine to pdf. Here is the solution that worked for me (see the link):

Is it possible to type Latex code and see its real time preview in LyX?

For your specific trouble, I've noticed that certain environments such as "aligned" can have issues.

One way to handle this is paste only the code inside the environment, following my solution in the link.

Next, after Lyx registers the equation (with its environment missing) correctly, copy the Lyx code you just pasted into some text editor. Now, add in the environment (e.g. \begin{aligned}, \end{aligned}) and any necessary formatting (e.g. =& tags) manually.

Now, with your environment added back in, when you paste this code in it will not register as text with the backslash issues. The reason is because when you copied the latex code the ascii text was encoded correctly. When you copy from other documents it may be encoded as a string (that's my best guess anyway).

Finally, any typos in your Latex code will not read correctly by lyx and it will appear as a string.

-