# How do I format words as variable names?

I am trying to find a way to make the equations that appear in this text a bit better looking... here is my suggestions:

    \documentclass{article}
\usepackage{amsmath}

\begin{document}
$$Risk_{i,t}=c+\alpha Risk_{i,t-1}+\beta_{1} CEO_\text{PC}_{i,t}+\beta_{2} BR\_ind_{i,t}+\beta_{k} Z_{i,t}+\epsilon_{i,t}$$
\end{document}


Here a caption of the equation that I'm looking for :

Some options you have for typesetting words as variables include:

### As Operator Names

The \operatorname command from amsmath typesets its argument like the operators sin or log. In particular, you get a bit of extra spacing between α and Risk: \alpha \Risk_i is typeset exactly like \alpha \log_i.

One downside is that the spacing between \Risk and \cdot will be wrong, so you would need to write something like \Risk\! \cdot \alpha, or {\Risk} \cdot \alpha.

\documentclass{article}
\usepackage{amsmath}
\usepackage{fontspec}

%Formatting for a MWE on TeX.SX:
\usepackage[paperwidth=10cm]{geometry}
\pagestyle{empty}

\newcommand{\Risk}{\operatorname{Risk}}
\newcommand{\CEOPC}{\operatorname{CEO\_PC}}
\newcommand{\BRind}{\operatorname{BR\_ind}}

\begin{document}
$$\begin{split} \Risk_{i,t} = c + &\alpha \Risk_{i,t-1} + \beta_{1}\CEOPC_{i,t} + \\ &\beta_{2}\BRind_{i,t} + \beta_{k}Z_{i,t}+\epsilon_{i,t} \end{split}$$
\end{document}


## As Formatted Text

You can use the command \textnormal in math mode to set short phrases of text, and use any text-mode formatting you want. In this example, I typeset them as slanted, not italic, text.

\documentclass{article}
\usepackage{amsmath}
\usepackage{fontspec}

%Formatting for a MWE on TeX.SX:
\usepackage[paperwidth=10cm]{geometry}
\pagestyle{empty}

\newcommand\variablename[1]{\mathop{\textnormal{\slshape #1}}\nolimits}

\newcommand{\Risk}{\variablename{Risk}}
\newcommand{\CEOPC}{\variablename{CEO\_PC}}
\newcommand{\BRind}{\variablename{BR\_ind}}

\begin{document}
$$\begin{split} \Risk_{i,t} = c + &\alpha \Risk_{i,t-1} + \beta_{1}\CEOPC_{i,t} + \\ &\beta_{2}\BRind_{i,t} + \beta_{k}Z_{i,t}+\epsilon_{i,t} \end{split}$$
\end{document}


If you used \text, as in your example, the formatting of the text preceding the equation would bleed through. In some situations, though, you might want that: if you’re using \Risk in a heading that’s typeset as bold sans-serif, you might want your math symbols in bold sans-serif too.

Wrapping it in \mathop gives you spacing like the operator \lim, but then subscripts would be set beneath like \displaystyle lim_{\epsilon \to 0}. So, inhibit this with \nolimits.

At Mico’s suggestion, I moved the formatting into a new command \variablename and used it to define the other macros. This also lets you change the formatting of all full-word variables in one place, and write \variablename{Return} without having to declare a macro.

### As Math Text Alphabets

This is what the alphabets \mathrm, \mathit, \mathbf, etc. are for.

\documentclass{article}
\usepackage{amsmath}
\usepackage{fontspec}

%Formatting for a MWE on TeX.SX:
\usepackage[paperwidth=10cm]{geometry}
\pagestyle{empty}

\newcommand\variablename[1]{\mathop{\mathit{#1}}\nolimits}

\newcommand{\Risk}{\variablename{Risk}}
\newcommand{\CEOPC}{\variablename{CEO\_PC}}
\newcommand{\BRind}{\variablename{BR\_ind}}

\begin{document}
$$\begin{split} \Risk_{i,t} = c + &\alpha \Risk_{i,t-1} + \beta_{1}\CEOPC_{i,t} + \\ &\beta_{2}\BRind_{i,t} + \beta_{k}Z_{i,t}+\epsilon_{i,t} \end{split}$$
\end{document}


By default and in most font packages, the shapes of the letters in \mathit are very similar to the math symbols in\mathnormal, but with \mathit, you get ligatures, kerning, and italic correction. You would definitely notice the difference between \mathit{fl} and \mathnormal{fl}.

### Declaring a New Math Font

You can declare new math alphabets like \mathrm and \mathit as well. In unicode-math, you would use \setmathfontface, and in legacy NFSS, you would use \DeclareMathAlphabet.

\documentclass{article}
\usepackage{amsmath}
\usepackage{iftex}

\iftutex
\usepackage{unicode-math}
\setmathfontface{\mathvar}{lmsans10-oblique.otf}[Ligatures={Common,Rare}]
\else
\usepackage[T1]{fontenc}
\DeclareMathAlphabet{\mathvar}{T1}{lmss}{m}{sl}
\fi

%Formatting for a MWE on TeX.SX:
\usepackage[paperwidth=10cm]{geometry}
\pagestyle{empty}

\newcommand\variablename[1]{\mathop{\mathvar{#1}}\nolimits}

\newcommand{\Risk}{\variablename{Risk}}
\newcommand{\CEOPC}{\variablename{CEO\_PC}}
\newcommand{\BRind}{\variablename{BR\_ind}}

\begin{document}
$$\begin{split} \Risk_{i,t} = c + &\alpha \Risk_{i,t-1} + \beta_{1}\CEOPC_{i,t} + \\ &\beta_{2}\BRind_{i,t} + \beta_{k}Z_{i,t}+\epsilon_{i,t} \end{split}$$
\end{document}


This example, which uses Latin Modern Sans Oblique, is somewhat contrived because either unicode-math or isomath define the alphabet \mathsfit.

### Update

Henri Menke in the comments had another good suggestion, saying he uses

 \newcommand*\diff{\mathop{}\!\mathrm{d}}


to get operator-like spacing on the left and ordinary spacing on the right in expressions like dx.

If you write the equations the way you did, these variable names should be typeset as operators. Not everyone thinks this is correct. If you do not, you should be careful to always write, for example, \alpha \cdot \Risk instead of \alpha \Risk: you do not want to typeset a·mass as amass.

• Always complete and nice your answer. +1. – Sebastiano Jul 12 at 19:16
• These are not operators, so the usage of \mathop is semantically wrong in my opinion. – Henri Menke Jul 13 at 4:39
• @HenriMenke Is there a better way to tell LaTeX to give them operator-like spacing? – Davislor Jul 13 at 4:46
• @Davislor Why should you? They are not operators. However, what I do for the differential operator d is \newcommand*\diff{\mathop{}\!\mathrm{d}}. This gives op spacing to the left but ord spacing to the right. But again, the differential operator is an operator, whereas these constructs here are not. – Henri Menke Jul 13 at 4:58
• +1. A minor suggestion: Instead of taking a direct approach to variable naming, which requires you to be willing to define lots and lots of macros (e.g., \Risk, \CEOPC, etc), consider taking an indirect approach. First, define a macro called, say, \vn (short for "variable name", I suppose) via, say, \newcommand{\vn}[1]{\textsc{#1}}; second, encase all variable names in \vn directives. That way, if you ever decide to change the appearance of variable names from small-caps to sans-serif, all you'll have to do is change the definition of \vn. – Mico Jul 13 at 11:04

Not entirely sure what you mean by a better-looking equation, but if you place all of your longer variable names in a text box, it would look better to my eyes!

    \documentclass{article}
\usepackage{amsmath}

\begin{document}

$$\text{Risk}_{i,t}=c+\alpha \text{Risk}_{i,t-1}+\beta_{1} \text{CEO\_PC}_{i,t} +\beta_{2} \text{BR\_ind}_{i,t}+\beta_{k} \text{Z}_{i,t}+\epsilon_{i,t}$$

\end{document}


Which would give:

• With this approach, you would want to use \mathrm instead of \text. That way, if your equation is inside something italicized (like an ams theorem), you'll still have the variables looking the same. With \text, they would inherit the italicization. – Teepeemm Jul 12 at 18:09
• True for many reasons. \mathrm is definitely preferred over \text. – Mark Verschell Jul 19 at 16:41

I add my proposal, to have another very nice view as output.

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}

\begin{document}
$$\mathsf{Risk}_{i,t}=c+\alpha \mathsf{Risk}_{i,t-1}+\beta_{1} \mathsf{CEO\_PC}_{i,t}+\beta_{2} \mathsf{BR}\_\mathsf{ind}_{i,t}+\beta_{k} \mathsf{Z}_{i,t}+\epsilon_{i,t}$$
\end{document}


Using a macro \newcommand{\vn}[1]{\mathsf{#1}} as suggested by @Mico in the comment to have not many \mathsf:

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amssymb}
\newcommand{\vn}[1]{\mathsf{#1}}

\begin{document}
$$\vn{Risk}_{i,t}=c+\alpha \vn{Risk}_{i,t-1}+\beta_{1} \vn{CEO\_PC}_{i,t}+\beta_{2} \vn{BR}\_\vn{ind}_{i,t}+\beta_{k} \vn{Z}_{i,t}+\epsilon_{i,t}$$
\end{document}

• You don't need sansmath for \mathsf. That package has a very different purpose. Also the image in the OP suggests \mathsf{CEO\_PC}_{i,t} instead of \mathsf{CEO}_{\mathsf{PC}_{i,t}}. – Henri Menke Jul 13 at 5:10
• @HenriMenke With lot of sincerity I not remember the reason because I have put sansmath. I have edited my answer with your suggestions, and I thank you again in other way. Best regards. – Sebastiano Jul 13 at 8:44
• +1. A minor suggestion: Instead of hardcoding the appearance of variable names (via \mathsf, say, take a two-step or indirect approach: In step 1, define a macro called \vn (short for "variable name", I suppose) via \newcommand{\vn}[1]{\mathsf{#1}}; in step 2, encase each variable name in a \vn directive. E.g., \vn{Risk}_{i,t}=c+\alpha \vn{Risk}_{i,t-1}+.... That way, if you decide to use upright-serif rather than upright-sanserif for variable names, all you'd have to do is change the definition of \vn. – Mico Jul 13 at 10:58