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).

  • 1
    write the complete code with ERT
    – user2478
    Commented Jun 20, 2012 at 10:50
  • 4
    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
    Commented Jun 20, 2012 at 12:04
  • @Herbert Yes, as I said, in the last paragraph, that would be an option...but an ugly one.
    – l7ll7
    Commented Jul 14, 2012 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!)
    – l7ll7
    Commented Jul 14, 2012 at 14:11

2 Answers 2


[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.)


The most confident way that this will work is save your tex code in a .tex file (making sure it compiles with your tex distribution) and then, import it into Lyx. There will be no "\backslashes" what-so-ever. Now you can copy what you just imported into any Lyx document anywhere you want.

A second way to handle this is paste only the latex code without the math environment.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .