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'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.
<to be read again> 
                   \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}
    \addtocounter{lineto}{-1}
    \lstinputlisting[linerange={1-\thelineto}]{
      #2
    }
    \addtocounter{linefrom}{\x}
    \addtocounter{linefrom}{-1}
    \addtocounter{lineto}{1}
    \lstinputlisting[linerange={4-\thelineto},style=highlight,firstnumber=last]{
      #2
    }
    \addtocounter{linefrom}{1}
  }
  \lstinputlisting[firstline=\the\value{linefrom},firstnumber=last]{
    #2
  }
}
\def\@without#1{
  \lstinputlisting[]{
    #1
  }
}

\makeatother

\begin{document}

\code[4]{hello.c}

\end{document}
share|improve this question
    
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

2 Answers 2

up vote 5 down vote accepted

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}
    \addtocounter{lineto}{-1}
    \lstinputlisting[linerange={1-\thelineto}]{
      #2
    }
    \addtocounter{linefrom}{\x}
    \addtocounter{linefrom}{-1}
    \addtocounter{lineto}{1}
    \begingroup\edef\y{\endgroup
      \noexpand\lstinputlisting[linerange=\thelinefrom-\thelineto,style=highlight,firstnumber=last]}%
     \y{#2}
    \addtocounter{linefrom}{1}
  }
  \lstinputlisting[firstline=\thelinefrom,firstnumber=last]{#2}
}

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

share|improve this answer
    
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.
<to be read again> 
                   \hfill 

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.


I copied the files that you linked and ran them without error in texlive 2012 producing:

enter image description here

share|improve this answer
    
Just added a MWE. –  Roberto Aloi Jul 27 '12 at 13:23

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.