Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Consider the following minimal example:

\documentclass{article}

\usepackage{glossaries}
\makeglossaries

\newcommand{\NewS}[4]{
    % Q1: How can I use argument #1 without the backslash in the following line?
    \newglossaryentry{symb:#4}{
        name=\ensuremath{#2},
        description={\nopostdesc #3}, 
        sort=sym#4,
    }

    % Q2: Why is the space between the '}' necessary?
    \def#1{\ensuremath{#2} } 
}

\NewS{\power}{P}{the power of something}{power}

\begin{document}
    One can refer to the symbol via \power and \gls{symb:power} and. 
\end{document}

The two questions I have are already inserted in the example. In particular I am searching for a way to remove the backlash of the string \power which I pass as a first argument to the newcommand so that I can write something like:

\newglossaryentry{symb:WITHOUT_BACKSLASH(#1)}{

And secondly I don't understand why I have to leave a space between the two braces in the \def in order to get "P and" in the output and not "Pand" - which is what I get when I remove the space. If I directly write \ensuremath{P} and I don't see any problems so I am just curious.

Thanks for your help!

Regards

share|improve this question
add comment

3 Answers

up vote 6 down vote accepted

It depends on how you need to use the control sequence name. A method for printing the control sequence name without the backslash is

\begingroup\escapechar=-1\edef\x{\endgroup\string\power}\x

In a definition, the explicit \power will be #1. But, again, it depends on what you want to do with the string.

The second question is a very frequently asked one: TeX ignores spaces after control sequence names like \power. Don't put a space in the definition, but rather write \power{} in the document.

If you just want to spare an argument, then use the LaTeX kernel function

\makeatletter
\newcommand{\NewS}[3]{%
    \newglossaryentry{symb:#1}{
        name=\ensuremath{#2},
        description={\nopostdesc #3}, 
        sort=sym#1,
    }%
    \@namedef{#1}{\ensuremath{#2}}%
}
\makeatother

Now \NewS{power}{P}{the power of something} will suffice and you will be able to say

\begin{document}
One can refer to the symbol via \power{} and \gls{symb:power} and. 
\end{document}
share|improve this answer
    
Thanks for answering the second question! –  Christian Jun 11 '11 at 11:54
    
Regarding the second question: I thought that \power is treated like a string when I use it as an argument of a command, e.g. in \NewS{\power}{...} above and that I can access this string via #1 in the definition of the command \NewS. Thus I understand \def#1{something} as \def\power{something}. I was therefore looking for a way to use the string without the backslash in the line \newglossaryentry{symb:<insert string 'power' here>}{. But I think that Ians answer is the better way to go. –  Christian Jun 11 '11 at 12:01
    
To avoid the {} everytime you use \power, you could include the {xspace} package, add \xspace to the end of the defintition of \power, and then use it as The \power is the \power. and should have a space before the is but not before the period. –  Peter Grill Jun 11 '11 at 17:26
    
Another option (which is slightly shorter and I think better because it doesn't look like it is passing an empty argument to a macro) is to use \ —that is, a backslash followed by a space (or new line)— after \power: ...via \power\ and .... –  TH. Jun 12 '11 at 10:19
add comment

In expl3, there is a \cs_to_str:N function to get the macro name without a backslash. If you are not familar with expl3 syntax, you can define a clone of the function:

\documentclass{minimal}
\usepackage{expl3}
\ExplSyntaxOn
  \let\cstostr\cs_to_str:N
\ExplSyntaxOff
\begin{document}
\cstostr\foobar
% We obtain: foobar
\end{document}

Note: \cs_to_str:N is fully expandable. Thus it's robust enough.

It's nontrivial to define a command like \cs_to_str:N. You can check the definition in source3 document, if you want to pry.

Here is my version of \cstostr:

\makeatletter
\def\cstostr#1{%
  \expandafter\@gobble\detokenize\expandafter{\string#1}}
\makeatother

The definition is much simpler than LaTeX3's \cs_to_str:N, but may fail when \escapechar is unusual. It is also fully expandable.


TeX ignores all spaces after a all-letter macro by default. To solve the question about space, you can use:

\power\ and

or

\power{} and
share|improve this answer
    
Thank you for your answer! –  Christian Jun 11 '11 at 13:31
add comment

An alternative approach is to create the macro from the string.

\documentclass{minimal}
\newcommand\makemacro[1]{\expandafter\def\csname#1\endcsname{arnold}}
\begin{document}
\makemacro{power}
\power
\end{document}
share|improve this answer
1  
Thanks, this works. And it seems to be the more natural approach. –  Christian Jun 11 '11 at 11:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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