Extract first & last characters of macro argument?

You could use the xstring package

\documentclass{article}

\usepackage{xstring}

\newcommand{\mymacro}[1]{%
    \StrLeft{#1}{1}[\firstletter]%
    \StrRight{#1}{1}[\lastletter]%
    First letter: \firstletter

    Last letter: \lastletter
}

\begin{document}
    \mymacro{ABCDEF}
\end{document}

I’m a little busy so please excuse that I didn’t build it in you MWE ;-)


Or in classic TeX; the following defines \fst and \lst to be the first and last characters (or be empty for short input)

\def\fl#1{\flx#1\empty\empty\empty}
\def\flx#1#2#3\empty{%
\edef\fst{#1}%
 \edef\cdar{#2}%
 \edef\cddr{#3}%
  \ifx\cddr\empty
    \let\lst\cdar
  \else
     \expandafter\flxx
   \fi
  #3}
\def\flxx#1#2\empty{%
 \edef\car{#1}%
  \ifx\car\empty
  \else
     \let\lst\car
     \expandafter\flxx
   \fi
  #2\empty}

\immediate\write20{===}
\fl{}\immediate\write20{[\fst][\lst]}
\immediate\write20{===1}
\fl{1}\immediate\write20{[\fst][\lst]}
\immediate\write20{===12}
\fl{12}\immediate\write20{[\fst][\lst]}
\immediate\write20{===123}
\fl{123}\immediate\write20{[\fst][\lst]}
\immediate\write20{===1234}
\fl{1234}\immediate\write20{[\fst][\lst]}

\end



===
[][]
===1
[1][]
===12
[1][2]
===123
[1][3]
===1234
[1][4]

Here are 3 solutions using expl3, depending on your requirements. To perform the tests, I'd use \prg_case_int:nnn rather than an ad-hoc bunch of \ifthenelse statements: those are slower.

(1) The simplest is to use \tl_head:n{#1} to access the first digit of the number, and \int_mod:nn {#1}{10} for the last one. This works for numbers less than 2^{31}, and with versions of expl3 from roughly June 2011 onwards.

\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\newcommand {\foo} [2] {
  \prg_case_int:nnn { \int_mod:nn {#1} {10} }
    {
      {1} { \kern .02em \relax }
      {4} { \kern .03em \relax }
      {7} { \kern -.02em \relax }
    }
    { }
  \prg_case_int:nnn { \tl_head:n {#2} }
    {
      {1} { \kern .02em \relax }
      {4} { \kern -.03em \relax }
      {5} { \kern .03em \relax }
      {7} { \kern .03em \relax }
    }
    { }
}
\ExplSyntaxOff

(2) With a recent version of expl3 (mid-january 2012 onwards), using \tl_item:nn is probably best, as it will work for any integer, and you probably don't really want to enforce that your numerator and denominator are integers anyways. Namnely, \tl_item:nn {#1} {<integer>} gives the <integer>-th token in the token list, starting at zero for the left-most token (first digit), with negative indices starting at -1 for the right-most token. In particular,

\tl_item:nn { abcd } { 0 } % => a, left-most token
\tl_item:nn { abcd } { 2 } % => c
\tl_item:nn { abcd } { -1 } % => d, right-most token
\tl_item:nn { abcd } { -5 } % => nothing: index out of bounds.

(3) If you need a solution which works both for arbitrarily long integers and for older versions of expl3, then you can replace the lines from \newcommand to \prg_case_int:nnn { \int_mod:nn {#1} {10} } by

%----->8---- cut here
\tl_new:N \l_foo_tl
\newcommand {\foo} [2] {
  \tl_clear:N \l_foo_tl
  \tl_map_inline:nn {#1} { \tl_set:Nn \l_foo_tl {##1} }
  \prg_case_int:nnn { \l_foo_tl }
%-----8<---- cut here

The idea is that I go through the string and store each character in \l_foo_tl, until reaching the end of the string. Hence, the last character remains in \l_foo_tl. That's a good replacement for \int_mod:nn {#1} {10} to get the last digit. However, the two other solutions I gave above are better long-term (especially the \tl_item:nn one).