4

I have a very long document. If I decided to rename a heavily used variable say $x$ to $X$ or $y$ or something else, would there be an easy way to rename the variable in my entire document? String replace doesn't help, since it replaces all my lower case x in the document.

6
  • Well, logical markup would help here: \newcommand{\myvar}{X} and $\myvar$. Of course, string replace is necessary anyway, but using it with $x$, although this would change occasions where $x$ should be kept (and x does have a different meaning --> query string replace is also possible)
    – user31729
    Aug 12, 2016 at 9:01
  • I belive that there was an aswer here be @egreg that changed the catocde in math mode for x so that it woud get highlited as red so you could easliy spot it. But, I can't seem to find it and need to go... I belive it also showed that an x in a macro name woudl not be affected (such as in \max). Aug 12, 2016 at 9:11
  • @ChristianHupfer No, wouldn't help, since the text is already written. ;-)
    – Rob
    Aug 12, 2016 at 11:56
  • @Rob: That's why I said you need a replace anyway ;-) Thinking in advance is always a good idea, so use logical markup. I 've done right from the beginning when I started with LaTeX almost 20 years ago!
    – user31729
    Aug 12, 2016 at 11:59
  • @ChristianHupfer I thought I rather think of good notation and names in advance, because equations gets unreadable in math mode when you have to use tons of macros. However, I failed...
    – Rob
    Aug 12, 2016 at 12:07

4 Answers 4

5

It's much better to do a replacement, but you can get away with

\documentclass{article}
\usepackage{amsmath}

\begingroup\lccode`~=`x
\lowercase{\endgroup\def~}{X}% or y or whatever as long as it doesn't contain x
\AtBeginDocument{\mathcode`x=\string"8000 }

\begin{document}

Extra $x=2\xrightarrow{f}3$

\end{document}

As you see, text is not affected, nor an x in a command name.

enter image description here

4
  • 1
    this is a very dirty trick, so i would not say it qualifies as "easily". Aug 12, 2016 at 13:03
  • 2
    @barbarabeeton For me it's easy. 😎
    – egreg
    Aug 12, 2016 at 13:19
  • 2
    i now that, but if if's not in the latex manual, it's not easy. what you suggests would appear in appendix d of the texbook. hence, by definition, not easy. Aug 12, 2016 at 13:33
  • @barbarabeeton ahahahah
    – percusse
    Aug 12, 2016 at 14:51
5

As Christian Hupfer already stated in his comment below the question it would be much better to define commands for variables in advance when it is not sure that the symbol will not change.

But if one has to do it later, WinEdt has a special RegEx syntax to find only stuff in math environments (which are known to WinEdt) which is

<something>\E{isMath}

So if you would like to replace all "x"es (in math environments) you could search for

x\E{isMath}

and simply replace it with e.g. n. Of course this would also find all "x"es (in math environments) so also e.g. the "x" in \max. Luckily there is the option Whole words only which indeed will only find "x" and not also "\max".

Here a screenshot of the Replace dialog showing the above search pattern and the used options.

screenshot of WinEdt's Replace dialog

1
  • Of course. I thought this was clear. But just in case it wasn't ,hopefully it is now. See my edited answer. Aug 12, 2016 at 20:49
3

Non TeX solution:

If x is a math variable name, it won't be preceded or followed by another letter, otherwise it will be another math variable. This statement can be translated in a regular expression so you can search and replace x with y using sed (there are sed ports for windows OSs as well).

This command changes x to y in a test.tex file making a backup file called test.tex.bak (but make your own backups as well, preferably as archives, and/or move them some place else):

sed -ri.bak 's/(^|[^a-zA-Z\\])x([^a-zA-Z]|$)/\1y\2/g' test.tex

For preliminary tests, you can add other 'x' combinations you may think you used in your document to see an immediate result:

sed -r 's/(^|[^a-zA-Z\\])x([^a-zA-Z]|$)/\1y\2/g' <<< 'add here --> x  exer  exxe 2x3 x^3 {x} xx 2x x2 $x$ \x \xtest maxx x\ x/'
1
  • Thank you! This seems to be the best solution. I am still checking my rather large paper, but it seems to have been a perfect 1-second solution.
    – linhares
    Jul 3, 2020 at 0:53
-1

You can replace $x$ with $X$. This does not affect the lower case x included in a text.

5
  • Yes, if all occurrence of x are in the form "$x$". But often this is not the case...
    – Paapaa
    Aug 12, 2016 at 9:11
  • When using a variable, you always should use math mode, even in text, to show the variable-"style".
    – der_user
    Aug 12, 2016 at 9:15
  • @der_user: What about \begin{align} x .... \end{align}? ;-) Neither your nor my approach with replace would work then
    – user31729
    Aug 12, 2016 at 9:17
  • @der_user: Yes, of course. But instead of $x$ OP could have $y + x^2$ or whatever. Replacing just $x$ wouldn't help.
    – Paapaa
    Aug 12, 2016 at 9:18
  • True, didn't think about that. My fault.
    – der_user
    Aug 12, 2016 at 9:21

You must log in to answer this question.

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