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?
Asked
Active
Viewed 324 times
2
-
5So 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 Answers
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
Jeffrey J Weimer
- 677
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
-
2Please don't use
$$, see https://tex.stackexchange.com/q/503 – samcarter_is_at_topanswers.xyz Nov 10 '18 at 21:57 -
-
-
I've seen that link even before this post, @samcarter. I use latex since 2015 and yet I feel quite new and with low level of knowledge about it. But during all my experience, I have never faced problem with either
$or$$. I still don't understand why such alarm about the dollar symbols for math mode. – Brasil Nov 10 '18 at 22:02 -
2A single
$sign for inline math is no problem at all (and I use it routinely), the problem is using$$for display math. I think the most notable problems caused by this are inconsistent spacing and left alignment of display maths won't work. – samcarter_is_at_topanswers.xyz Nov 10 '18 at 22:09 -
Good. I will adopt the other methods like
\(and\[. I already use\begin{equation}for enumerated equations. Thank you, @samcarter. – Brasil Nov 10 '18 at 22:15 -
2No need to use
\(, a single$is fine for inline math, just don't use$$– samcarter_is_at_topanswers.xyz Nov 10 '18 at 22:22 -
1See also this post for the distinction between using \newcommand and \def. – Jeffrey J Weimer Nov 10 '18 at 22:56
-
1Also note that the spaces after
\Xare eaten in text mode, so the output in your example is notx raisedbutxraised– siracusa Nov 10 '18 at 23:05 -
3
zzz \X\ zzzdoesn't actually save any characters over the simpler and quickerzzz $x$ zzz– David Carlisle Nov 10 '18 at 23:49