2

I have this bit of LaTeX code.

f(n)=\begin{cases}n/2&\mbox{if }n\equiv0\\(3n+1)/2&\mbox{if}n\equiv1.\end{cases}\pmod{2}    

I want to see it rendered in my LyX document. If I copy and paste it into the "Insert Math" box given by Ctrl+M, it works and I see the this in my document

I would like to be able to manually type that code (and code in general) into LyX and get the same result. That is, I would like to be able to manually enter code into LyX and have it have the same effect as pasting that code into LyX. I can't find a way to do this. My current workaround is to keep a separate text editor open from which I paste into LyX, which is not ideal.

If I try to type the same code by hand into a math box, I get a mess whose beginning looks like this:

The same happens if try to type it into a "Insert -> TeX code" box nested inside the math box.

If I type or paste it into a code box not nested inside a math box, I get this:

What am I doing wrong? Based on googling, the "instant preview" setting seems to be connected. I've turned it on and off and see no difference. I'm using LyX 2.12 with all default settings (except for the instant preview).

  • If you are happy to type latex code why not type the whole document as latex rather than use lyx (that is what most people here do) (I've never used lyx so perhaps there are advantages but I only really see it as of use for people who don't know latex markup) – David Carlisle Nov 14 '14 at 11:44
  • @Christian: This is more of a user interface issue. I can't think of a way to illustrate it with a complete document source. – Joe N Nov 14 '14 at 12:23
  • @David: That's an option. But I like to work in Lyx's WYSIWYG mode as well. I'm looking to have the best of both worlds in this case - the speed of WYSIWYG and the precision of code - since it seems like it should be possible. – Joe N Nov 14 '14 at 12:26
1

The reason why pasting is different from typing is because when you paste, LyX tries to detect what content you are pasting. It can detect LaTeX, for example, which is what it does in this case and it then runs tex2lyx to import the LaTeX into LyX's format (although for math there is not much conversion that needs to be done I believe). When you type, LyX sees it as character by character, so it does not try to detect the format.

You might be interested in preview boxes. In your TeX Box example, select the red box (the box, not what's inside the box) and then go to Insert > Preview. That will put a black box around the red box. Note that your TeX Code is incomplete as it is. You have to surround it with $ (or similar), just like in LaTeX. After you have the preview box, move click outside so that LyX knows to refresh the box. Then you should see your expression as LaTeX would display it.

Below I show a screen shot with duplicate expressions. That is, both are a TeX box inside of a preview box. The only difference is that in the top one I have my cursor inside it.

enter image description here

I'm not sure why there is no space between "if" and "n". Did you make a mistake in your post or did I make a mistake?

  • This does pretty much what I want, thank you! The mistake was mine. – Joe N Nov 15 '14 at 9:09
4

The simple, factual answer is: you can't do that. There is no hidden option that will make math entry work precisely the same way as LaTeX, even though it works for copy and paste.

However, even without changing any options, it does behave almost like LaTeX, but there are some tricks to get used to. I suspect the second point will mostly answer your questions, but maybe it's helpful to list the other things too.

  • If you want to enter a simple LaTeX command like \alpha or \equiv, then you can just enter it directly, as you seem to have already noticed.
  • To insert an environment like \begin{cases} ... \end{cases}, you must type \cases. This is rather odd, but it does save typing.
  • Once you've entered a command that accepts a parameter (such as the cases environment or \mbox) don't type an open brace; just press space. As with your pasted equation, instead of seeing all the braces that get sent to LaTeX you will just see boxes where you need to enter the parameters.
  • Conversely to the previous point: don't press space to literally enter a space. It will not send a space to LaTeX (which is usually done to make your LaTeX code neater, but since you never see it in LyX there's no need) nor will it make an actual space (use insert->formatting->horizontal space for this, or type the relevant command directly, such as \, or \quad). LyX interprets space to mean "finish this command" if you've started typing one, or "take me outside this command parameter (or equation)" otherwise.
  • Instead of using & and \\ to create more rows and columns, use the toolbar buttons (the four buttons just to the left of the button with a pi on it).
  • Another note about braces. If you type a brace in an equation then LyX will automatically output \{ or \} to LaTeX so it looks like a brace in the final output. If you really want to send a brace to LaTeX then you instead need to type \{ (yes this is the opposite way round to LaTeX!). You will know if you've done this correctly because the close brace will show up automatically and both will be red.
  • Yet another note about braces! There is an exception to the above: if you type an open brace after a command but before pressing space, then LyX will insert the raw LaTeX even without you typing a \. You can see that this has happened in your screenshot. This is useful if you've just typed a command that LyX doesn't recognise, so it can't handle the parameters for you, but annoying if it's a command that LyX can handle itself, as happened when you typed \mbox{.
  • Many of the things above have keyboard shortcuts, usually starting with Alt+M. For example, Alt+M followed by g then a gives \alpha, and Alt+M followed by f gives \frac. Ctrl+Enter is useful for adding to rows to things (like cases environments, multiline equations and matrices).

One final point, unrelated to LyX. For entering text into an equation it is better to use \text rather than \mbox. This uses the correct font size in a script, and uses the settings of the surrounding text e.g. emphasised in a theorem environment.

Instant preview is not related to entering equations. It just means that when you move the cursor entirely out of an equation then it will be sent through LaTeX as a mini document and shown in LyX (until you put the cursor back in the equation) so that you can see precisely how it will be output, rather than seeing LyX's approximation of the equation output.

  • This is very helpful! – Joe N Nov 15 '14 at 9:13

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.