1

I would like a file of latex commands to be rendered in LyX's editor.

Variations of this question exist all over the internet and tex stack exchange, but the answers seem either inconsistent, not sufficiently explicit, or outdated.

Suppose I create a file called mycommands.tex

% Tex commands
\newcommand{foo}{\alpha}
\newcomman{bar}{\beta}

Then, in the LyX preamble, I do \usepackage{mycommands} or \input{mycommands.tex}

is there a way to get these user-defined commands to render/preview? To be even clearer, if I type control+M, then \alpha in LyX, it will render an $\alpha$ right in my editor. If I type control+M then \foo, will it also render?

LyX must get these definitions from somewhere. And presumably people running Linux should have access to all the installation files, and there must be a way to define new commands that render in LyX.

This one is related: tex stack exchange post

Thanks!

3
  • i'm not sure if it's what you're looking for, but there's also "LyX macros". I don't know much about them but you can read the help documents to learn more. – scottkosty Jul 9 '19 at 11:00
  • 1
    @scottkosty: probably not. You're probably thinking of LyX macros defined within a LyX document, which aren't useful if you have a long file of LaTeX \newcommand's that you want to use. Suppose you're writing an academic paper or working with a collaborator and are given a commands.tex file containing commands that you are supposed to use. To use vanilla LyX macros, you'd need to convert all the LaTeX commands into LyX macros, which as far as I know isn't straightforward or automated in any way. Crucially, every time the "commands.tex" file changed, you'd need to do the conversion again. – RMurphy Jul 9 '19 at 13:17
  • Makes sense. Thanks for the explanation! – scottkosty Jul 9 '19 at 17:06
2

I think I've got this figured out. Yes, it is possible to render commands defined in the preamble, even when they are defined in loaded from another file. I am using LyX Version 2.3.2 and Ubuntu 16.04 LTS. They key is instant preview.

Turn on instant preview

Tools >> Preferences Look and Feel >> Display

  • If the option is not there, try following the steps of these instructions, where you are asked to install preview.sty and check other dependencies
  • preview.sty may not included in your LaTeX distribution and you might get errors using tex-live manager (tlmgr) to install it. To make a long story short, it seems you can't install new packages with old LaTeX distributions, at least in Ubuntu. I had success completely wiping my LaTeX distribution and installing the latest version from scratch, following these extensive instructions

Create an .sty file

List your commands in a file suffixed with ".sty". It is as simple as that, for example, mine looks like

% my commands.sty file
\newcommand{\foobar}{\beta}

Load the sty file

In the preamble (Document >> Settings >> LaTeX Preamble), call

% in the preamble: notice the suffix .sty is removed
\usepackage{path/to/commands} 

make sure not to type out the "sty" suffix in the preamble. There are no special headers, footers, etc in the sty file.

Type your new commands in LyX

Enter a math formula using your special commands as you usually would (Control+m), then move the cursor out of the math environment. After a few seconds, the commands will render. They do not appear in blue like typical LyX math, but in black.

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.