9

I've created a custom command that takes two arguments and uses one of them as a subscript. Obviously, the subscript has to be in math mode. Unfortunately, it doesn't put the argument in subscript. I can make the subscript work when done manually, but not in my custom command.

I'm at a lost to why this isn't working.

MWE:

\documentclass{article}
\usepackage{mwe}

\newcommand{\var}[1]{\textsf{#1}}
\newcommand{\LOC}[2][]{\var{LOC#2}$_{#1}$}
\begin{document}

\LOC{C}---Without optional argument.

\var{LOCC}$_{1}$---This looks correct.

\LOC{C}{1}---What happened here?
\end{document}

enter image description here

  • 2
    you're using the wrong syntax. the optional argument should be in square brackets, not braces, and be input before the main argumant, not after. here's what you want: \LOC[1]{C}. – barbara beeton Dec 20 '17 at 22:12
  • 1
    @barbarabeeton Of course, I am. I guess I need more sleep. Make your comment an answer and I'll accept it. – jlconlin Dec 20 '17 at 22:21
11

you're using the wrong syntax.

the optional argument should be in square brackets, not braces, and be input before the main argument, not after.

here's what you want: \LOC[1]{C}.

6

For optional arguments in curly braces and in a trailing position you need xparse. Also, you probably want to use \textsubscript.

\documentclass{article}

\usepackage{xparse}

\NewDocumentCommand\var{m}{\textsf{#1}}
\NewDocumentCommand\LOC{mG{}}{\var{LOC#1}\textsubscript{#2}}

\begin{document}

\LOC{C}---Without optional argument.

\var{LOCC}\textsubscript{1}---This looks correct.

\LOC{C}{1}---What happened here?

\end{document}

enter image description here

4

Your use of optional arguments doesn't match what is expected. Specifically, optional arguments use [..] while mandatory arguments use {..}. Also, \newcommand{<cmd>}[<num>][<opt>]{<stuff>} requires the optional argument to be placed before the mandatory one:

<cmd>[<opt>]{<mandatory>}...

If you wish to have the input you're currently using - specifying the optional argument at the end - you can use the following options:

enter image description here

\documentclass{article}

\makeatletter
\newcommand{\var}[1]{\textsf{#1}}
\newcommand{\LOCa@}[1][\relax]{\ifx#1\relax\else\ensuremath{_{#1}}\fi}
\newcommand{\LOCa}[1]{\var{LOC#1}\LOCa@}
\makeatother

\usepackage{xparse}

\NewDocumentCommand{\LOCb}{m o}{%
  \var{LOC#1}%
  \IfValueT{#2}{\ensuremath{_{#2}}}%
}

\begin{document}

\LOCa{C}---Without optional argument.

$\var{LOCC}_{1}$---This looks correct.

\LOCa{C}[1]---What happened here?

\LOCb{C}---Without optional argument.

$\var{LOCC}_{1}$---This looks correct.

\LOCb{C}[1]---What happened here?

\end{document}

The xparse interface is easier to understand. Additionally, consider coding your macros without the forced switches (to math mode) in mind. Rather switch to math mode where you use it within your document:

$\LOC{C}[1]$

Reference:

  • I used \ensuremath because I kept getting the error that I can't use \textsf in mathmode. – jlconlin Dec 20 '17 at 22:39
  • @jlconlin: You can use $a\textsf{b}c$ or \ensuremath{a\textsf{b}c} without problem. – Werner Dec 20 '17 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.