# Improper alphabetic constant

I'm using two counters (linefrom and lineto) to specify the range of lines to be printed in my listing:

  \setcounter{linefrom}{1}
\setcounter{lineto}{5}


The following passes compilation and works as expected:

\lstinputlisting[linerange={1-\value{lineto}}]{
...
}


Whilst the following generates an error:

\lstinputlisting[linerange={\value{linefrom}-\value{lineto}}]{
...
}


The error being:

! Improper alphabetic constant.
\value


Could someone enlighten me, please? A Minimal Working Example is available at: https://gist.github.com/3187943

\documentclass [11pt,oneside,onecolumn]{article}
\usepackage{listings}
\usepackage{tikz}
\usepackage{filecontents}

\begin{filecontents*}{hello.c}
#include <stdio.h>

int main(void) {
printf("Hello World!\n");
// return 0;
}
\end{filecontents*}

\lstset{
numbers=left,
basicstyle=\footnotesize,
aboveskip=0pt,
belowskip=0pt,
showlines = true
}

\lstdefinestyle{highlight}{
backgroundcolor=\color{orange}
}

\newcounter{linefrom}
\newcounter{lineto}

\makeatletter
\def\code{\@ifnextchar[{\@with}{\@without}}
\def\@with[#1]#2{
\setcounter{linefrom}{1}
\foreach \x in {#1}{
\setcounter{lineto}{\x}
\lstinputlisting[linerange={1-\thelineto}]{
#2
}
\lstinputlisting[linerange={4-\thelineto},style=highlight,firstnumber=last]{
#2
}
}
\lstinputlisting[firstline=\the\value{linefrom},firstnumber=last]{
#2
}
}
\def\@without#1{
\lstinputlisting[]{
#1
}
}

\makeatother

\begin{document}

\code[4]{hello.c}

\end{document}

-
Not without a real example. –  egreg Jul 27 '12 at 12:46
Just added a MWE. Lines 25 and 31 contain hard-coded values. Changing them to \thelinefrom provokes the error. –  Roberto Aloi Jul 27 '12 at 13:23
I've combined your files and they don't give any errors. Can try to replicate the error on what I have included above? –  percusse Jul 27 '12 at 13:36
As I just said, to reproduce the error simply replace the hardcoded values in lines 25 and 31 (my.sty) to \thelinefrom. I've created a new revision with this change for you: gist.github.com/3187943/… –  Roberto Aloi Jul 27 '12 at 13:49

It seems that linerange wants to see an explicit number as the start. So we present it one:

\def\@with[#1]#2{
\setcounter{linefrom}{1}
\foreach \x in {#1}{
\setcounter{lineto}{\x}
\lstinputlisting[linerange={1-\thelineto}]{
#2
}
\begingroup\edef\y{\endgroup
\noexpand\lstinputlisting[linerange=\thelinefrom-\thelineto,style=highlight,firstnumber=last]}%
\y{#2}
}
\lstinputlisting[firstline=\thelinefrom,firstnumber=last]{#2}
}


The \edef will expand completely \thelinefrom and \thelineto.

-
This did the trick. I've updated the gist with a working version, in case it is needed by someone else. –  Roberto Aloi Jul 27 '12 at 14:36

I can tell you what the error means at least. an alphabetic constant uses the backtic to get the character code of a letter:

\$ tex
This is TeX, Version 3.1415926 (TeX Live 2012)
**\relax

*\count0=a

*\showthe\count0
> 97.


The error you show is what you get if you put a command after the backtick that does not expand to a character token:

*\count0=\hfill
! Improper alphabetic constant.

As no MWE is supplied I haven't tried to reproduce but you might to better with \thelinefrom and \thelineto rather than use \value as they will expand to character tokens of the digits in the value.