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.

I have a custom macro like this that stores data:

\somecommand{some text}{234}

Inside the newcommand for this, I can access the second column of data using #2.

  • How can I obtain the first digit of that number? For e.g.:

\somecommand{some text}{3012} gives 3

\somecommand{some text}{21231} gives 2

\somecommand{some text}{9} gives 9

share|improve this question
2  
What does \somecommand look like, since \somecommand does not have a #3 argument? –  Werner Nov 28 '11 at 1:07
    
Oh, I made an error while writing it, I will fix that. –  Village Nov 28 '11 at 1:11

4 Answers 4

up vote 11 down vote accepted

You could use \StrLeft from the xstring package:

\documentclass{article}
\usepackage{xstring}

\newcommand\somecommand[2]{%
  \StrLeft{#2}{1}}
\begin{document}

\somecommand{some text}{3012}

\somecommand{some text}{21231}

\somecommand{some text}{9}

\end{document}
share|improve this answer

The usual approach using TeX programming would be

\makeatletter
\newcommand{\somecommand}[2]{%
  \ifx\relax#2\relax
    \expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
    {\GetFirstDigit#2\stop}%
}
\makeatother
\def\GetFirstDigit#1#2\stop{#1}

where \stop is 'something that will not appear in #2'.

share|improve this answer

A general solution without xstring (but tricky):

\def\helper#1{\let\temp#1\iffalse}
\newcommand\somecommand[2]{%
  \helper#2\fi
  \temp}

Joseph Wright pointed that it may fail if #2 is empty. However, it does work.

When we use \somecommand{foo}{}, we have

#1->foo
#2->(empty)

Then \somecommand{foo}{} expands to

\helper\fi
\temp

And then, \helper\fi expands to

\let\temp\fi\iffalse

So we have

\let\temp\fi\iffalse
\temp

Thus

\iffalse\fi

Anyway, Joseph's solution is better. My \somecommand is not expandable, it may fail in a \edef.

share|improve this answer
1  
Runs away if #2 is completely empty. –  Joseph Wright Nov 28 '11 at 7:08
    
@JosephWright: I'm sorry I didn't realized that case. However, \somecommand{foo}{} does works. \temp will be \fi (I don't know why) then \somecommand{foo}{} becomes \iffalse\temp and then \iffalse\fi. I;ll be glad if you can explain this. Maybe I'll post another question. –  Leo Liu Nov 28 '11 at 9:06
    
OK, I understand it now. I'll add some explanations. –  Leo Liu Nov 28 '11 at 9:13

You can simplify, if you wish using a macro with delimited parameters \def:

\def\somecommand #1|#2#3|{%
  #2
}
\somecommand Plenty of text|1234567|

I use | as a marker, but you can use anything you like. If there is no chance of a semicolon in the text, I would use a semicolon. This type of macro definition was quite a favourite with Knuth and there are many examples in the TeXbook. If the text is longer than a paragraph use \long\def.

share|improve this answer

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.