2

I would like to add a command to my document so that I could write 'X' in a document and it would be interpreted as '$x$' by compiler (because x is a mathematical variable that I use a lot). Can you help me?

Piotr
  • 21
  • 5
    So Xavier would become $x$avier? Would \X be acceptable? – John Kormylo Nov 10 '18 at 20:27
  • Welcome to TeX.SE. Please tell us which LaTeX compiler -- pdflatex, xelatex, lualatex, or something else? -- and which document class you employ. – Mico Nov 10 '18 at 20:41

2 Answers2

2

You cannot/should not just type X for translation. You should plan at least to type \X (better still \X{}). You will want this in the preamble of your document.

\newcommand{\X}{$x$}

Better still is below. This allows the math to be correct also in otherwise fragile commands.

\newcommand{\X}{\(x\)\relax}

The Find/Replace option in your editor is needed to make the changes in an existing document.

Find $x$  ... Replace with \X
1

I would suggest you to define the macro \def\X{\ensuremath{x}}. It will work both in the text and in the math mode as well. See the example below

You can show that three times \X\ raised to the second power is
$$
    (3\X)^2 = 9 \X^2
$$

You'll see that it outputs something similar to

You can show that three times x raised to the second power is

(3 x)² = 9 x²

Brasil
  • 1,286