Math & ScienceVariable Names

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

Variable Names

Post by topsquark »

I'm trying to learn how to use TikZ without going into tikzmath all of the time (It's useful, but I'm starting to see it as a crutch) and I'm having a lot of problems with the variable names (macros?)

I try to define things like \p, or \th1 or something (using def, newcommand, and pfgmathsetmacro), and I keep getting the error "Use of th does not match its definition". I never tried to define \th, I tried to define \th1. Aarrgghh!

I've come to the conclusion that I can't define anything that starts off with something already in use, such as \theta. (But I can use \s. I don't get it.)

Is there anything on the web about this? I can't find it.

Thanks!

-Dan

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
Stefan Kottwitz
Site Admin
Posts: 10319
Joined: Mon Mar 10, 2008 9:44 pm

Variable Names

Post by Stefan Kottwitz »

Hi Dan,

\th1 is not a valid TeX macro name. A quick reference: What are (TeX) macros says "Macro names are conventionally built from a \ followed by a sequence of letters, which may be upper or lower case" and Non-letters in macro names says it more clearly and explains a complicated way to work around it.

Stefan
LaTeX.org admin
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

Variable Names

Post by topsquark »

Stefan Kottwitz wrote:Hi Dan,

\th1 is not a valid TeX macro name. A quick reference: What are (TeX) macros says "Macro names are conventionally built from a \ followed by a sequence of letters, which may be upper or lower case" and Non-letters in macro names says it more clearly and explains a complicated way to work around it.

Stefan
Ahhhh! That explains a lot. However I note that I can define a path and call it c1, but you are saying I cannot call a macro (what I've been calling a variable) c1. (Or rather, that I will run into a lot of havoc if I try).

But why am I having a problem with \th? I can see why I might have a problem with \rad (it's already defined as something else). Is \th already in use for something? If so, is there a list somewhere of all LaTeX macros? Such a dictionary would be of immense use to me in any case. I should note that I currently can't afford a LaTeX book: I just purchased a rather expensive Differential Geometry text and am saving up for "part 2" which is twice as expensive. I have my priorities, I guess, so if the answer is a book I'll have to wait to get my answer!

Note: I do search the web for manuals but, as my ex-fiance could have told you, my web searching skills are as good as my Spanglish: Ellas son awful!

For the record, in my latest project I used \thoneone ("theta11") and I previously used things like \rIV ("radius4"). Letters only. Thank you!

Thank you for the FAQ site. That should help me a lot.

-Dan
rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Variable Names

Post by rais »

Hi Dan,
topsquark wrote: But why am I having a problem with \th?
actually, this is already explained in Stefan's second link (see its technique 4), never mind that it's about `\cul8er' (as in: `see you later')
topsquark wrote: Is \th already in use for something?
yes, \th defined by the LaTeX Kernel produces a glyph that looks like a `b', overprinted by a `p', if T1 encoding is in effect, at least.
However, just the fact that \th is already defined wouldn't stop a command like \def to overwrite its definition.
Let's have a closer look:
(Note: the following code intentionally produces an error, should `halt-on-error' be in effect you may need to convince the compiler to continue before you get any output)

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}
\begin{document}
\th\ (original)

\begingroup
  \def\th1{foo}%
  \th1% this works
  \def\th2{bar}%
  \th
      2% this works, too

  but:
  \th1% this will lead to your error

  \renewcommand*\th3{baz}% this works again, but differently from what one might expect;-)

  \th3
  
\endgroup
\th\ (back to original)
\end{document}
What's happening here is:
The first call to \th (line 4) just shows what \th would produce in its original definition (which is not available in OT1 encoding, hence line 2).
The grouping (\begingroup, line 6 and \endgroup, line 20) is just a stand-in for any LaTeX environment---most likely `tikzpicture' in your case---to demonstrate that \th will revert to its original definition afterwards (line 21).

Code: Select all

\def\th1{foo}
then (re)defines not \th1, but \th, to be followed by `1', after which a call to `\th1' just works fine (line 8 produces `foo' in the output).

Code: Select all

\def\th2{bar}
(line 9) now redefines \th to be followed by `2' and to contain `bar', after which a call to \th2 (lines 10 + 11) is still valid, even if there is a bit of white space between \th and 2.
That's why a call to \th1 after above definition (line 14) fails with

Code: Select all

! Use of \th doesn't match its definition.
l.14   \th1
Assuming you managed to bypass above error condition,

Code: Select all

\renewcommand*\th3{baz}
(line 16) redefines \th to contain 3, but discards `baz' from this definition, that's why `baz' is output even before `\th3' is called.
Calling `\th3' after above \renewcommand (line 18) would then lead to `33' in the output: \th contains 3, which is followed by a 3...
topsquark wrote: If so, is there a list somewhere of all LaTeX macros?
I doubt it. Keeping track of macros defined by TeX, LaTeX, and all packages ever created for use with LaTeX, sounds almost impossible to me. The closest match I can think of would be the LaTeX Companion in its latest edition, which is a great book; even your \th is listed in it on page 462 (2nd edition), by the way.
OTOH, the TeX primitives aren't likely to change, so https://www.tug.org/utilities/plain/cseq.html or `texdoc TeXbyTopic' should provide sufficient information about the macros that are 'under the hood', when using (La)TeX.
What's defined in the LaTeX Kernel can be accessed by `texdoc source2e'.
What's defined by a package can be accessed by `texdoc <name-of-package>'

KR
Rainer
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

Variable Names

Post by topsquark »

Thank you, both.

-Dan
Post Reply