Math & ScienceProgramming Language Variables

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
yoopits
Posts: 2
Joined: Wed Sep 28, 2011 3:10 am

Programming Language Variables

Post by yoopits »

(I'm new to TeX and LaTeX) I would like to create math expressions with multi-letter variable names as used in programming languages. For example, in
a^2 + efg^2
I would like "efg" to be treated like a single variable, just like "a".

Can anyone help? Is there something I should read concerning this?

Recommended reading 2024:

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

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

Frits
Posts: 169
Joined: Wed Feb 02, 2011 6:02 pm

Re: Programming Language Variables

Post by Frits »

Please be more specific, I can't help you right now since I don't understand the question.
howtoTeX.com - Your LaTeX resource site (Tips, Tricks, Templates and more!)
Follow howtoTeX on twitter
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Programming Language Variables

Post by Stefan Kottwitz »

In math mode, letters are treated like variables, that's why the spacing in math is different from spacing in text, as you noticed. You could use \mathit for example, or \mathrm for upright symbols (like for operators).

I recommend writing a macro for your variables, so you can easily change the appearance in the complete text by modifying the macro.

Code: Select all

\documentclass{article}
\newcommand*{\var}[1]{\mathord{\mathit{#1}}}
\newcommand*{\efg}{\var{efg}}
\begin{document}
Compare $efg^2$ versus $\efg^2$
\end{document}
variable.png
variable.png (3.1 KiB) Viewed 4796 times
Now, even if you define a lot of variables, one single macro is responsible for the appearance in text, so you can still fine tune if required.

Stefan
LaTeX.org admin
yoopits
Posts: 2
Joined: Wed Sep 28, 2011 3:10 am

Re: Programming Language Variables

Post by yoopits »

I figured that efg was being treated like 3 variables; or perhaps that the f was being treated like the symbol for "function". I also ran across \mathrm, \mathit, and so forth.

The main thing, though, was to find a way to define a multi-letter variable. So thank you for the macro. That gives me an immediate solution, and an area to delve into for more ideas. Also, thanks for the hint about formatting variables differently.
Post Reply