0

As a continuation of thread https://tex.stackexchange.com/questions/233658/2010-2014-versions-difference:

I am trying to decorate program listings with highlighted lines, numbered balls, etc. In 2010, I used a macro which does not work any more with 2014 TeX (OSX). In the code I do have a macro call

\MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}

which should highlight lines 1-3 of the source code listing. As given in the MWE, it works (although I think the positioning changed somewhat). I want to pass this macro to the listing macro as "decoration", in order to put it inside the figure environment. I use it in the macro as

\begingroup\edef\x{\endgroup\noexpand\lstinputlisting[label=#4, name=#4,\ME@options] {#2}
    {\ME@decorations} % Decorating comments
    }\x

and pass it as

\MESourceFile[language={[x86masm]Assembler},wide=false
,options={
%\MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}
}

Without the comment, of course. I guessed that this way is equivalent with the direct call. However, I receive

! Undefined control sequence.
\pgfmathsetcount ... \pgfmath@onquick #2\pgfmath@ 
                                                  {\afterassignment \pgfmath...
l.192 ...loWorld.c}{Hello world}{lst:HelloWorld.c}

What is wrong?

\documentclass{book}
\usepackage{xcolor}
\usepackage{xkeyval}
\usepackage{listings}
    \usepackage{tikz}   \usetikzlibrary{shapes,positioning,arrows,fit,backgrounds,calc,tikzmark,shadows}%
%   \usepackage[customcolors]{hf-tikz}
%   \usepackage{pdfrender}

\definecolor{ForestGreen}{rgb}{0.0, 0.4, 0.0}
\definecolor{NavyBlue}{rgb}{0.0, 0.0, 0.5}
%\definecolor{firebrick}{rgb}{0.7, 0.13, 0.13}
\definecolor{burntorange}{rgb}{0.8, 0.33, 0.0}
\definecolor{cream}{rgb}{1.0, 0.99, 0.82}
\newlength{\mywidth} % Used in defining listing width
\definecolor{ivory}{rgb}{1.0, 1.0, 0.94}
    \colorlet{SeparatorColor}{burntorange}
\def\lstsize{\scriptsize}
%% Set up the professional-looking program language environment
%% #1 is the language specification
%% #2 is the optional arguments
%% Usage \MESetListingFormat[numbers=left]{[ANSI]C}
%%
\newcommand\MESetListingFormat[2][] % Optional is the extra arguments
{
    \lstset{
        language={#2},      % Select the language   
%         numbers=left,               % Place for line numbers
         numberstyle=\tiny,          % Style of line numbers
         numbersep=5pt,              % Distance of line numbers from source
         tabsize=2,                      % Size of Tabs
         inputencoding=utf8/latin2, % input encoding, allow Hungarian umlauts
         extendedchars=true,         %
%        texcl=true,                    % Allow accented chars in comments
         escapechar=\@,
        breaklines=true,        % sets automatic line breaking
        breakatwhitespace=true,    % sets if automatic breaks should only happen at whitespace
        escapeinside={\%*}{*)},          % if you want to add a comment within your code
        frame=tb, 
        framerule=.5pt, 
        firstline=1,lastline=5,
%       rulecolor= \color{green!50!black!20},
%       rulecolor= \color{burntorange!50},
        rulecolor= \color{SeparatorColor},
         backgroundcolor=\color{ivory},
       basicstyle=\ttfamily\color{black}\lstsize , 
         keywordstyle=\bfseries\color{magenta},
         identifierstyle=\bfseries\color{NavyBlue},
        commentstyle=\itshape\bfseries\color{ForestGreen},
        stringstyle=\itshape\bfseries\color{burntorange}, % Color for strings
          lineskip=0pt,aboveskip=4pt,belowskip=2pt,
            framesep=4pt,rulesep=2pt, %framerule=.25pt,
          showspaces=false,           % 
         showtabs=false,             % 
         xleftmargin=-1pt,
         framexbottommargin=4pt,
         framextopmargin=4pt,
         gobble=5,
%         title=\lsttitle{MyTitle},
        framexleftmargin=0pt,
        framexrightmargin=0pt,
         showstringspaces=false      % Show empty spaces?   
    }   %Take optional arguments
    \lstset{#1}
}%MESetListingFormat

    \makeatletter
    % % % ========= Macros using kexval =========
    % ========= KEY DEFINITIONS =========
    \define@key{MEMacros}{color}{\def\ME@color{#1}}
    \define@key{MEMacros}{decorations}{\def\ME@decorations{#1}}
    \define@key{MEMacros}{iconheight}[10pt]{\def\ME@color{#1}}
    \define@key{MEMacros}{language}{\def\ME@language{#1}}
    \define@key{MEMacros}{number}{\def\ME@number{#1}}
    \define@key{MEMacros}{options}{\def\ME@options{#1}}
    \define@key{MEMacros}{plain}[true]{\def\ME@plain{#1}}       
    \define@key{MEMacros}{resize}[true]{\def\ME@resize{#1}}
    \define@key{MEMacros}{shrink}[true]{\def\ME@shrink{#1}}     
    \define@key{MEMacros}{tall}[true]{\def\ME@tall{#1}}
    \define@key{MEMacros}{title}{\def\ME@title{#1}}
    \define@key{MEMacros}{wide}[true]{\def\ME@wide{#1}}
    \define@key{MEMacros}{width}[3cm]{\def\ME@width{#1}}
    \define@key{MEMacros}{color}[green]{\def\ME@color{#1}}
    \presetkeys{MEMacros}{color=green}{}%
    \presetkeys{MEMacros}{number=1}{}%
    \presetkeys{MEMacros}{plain=false}{}%
    \presetkeys{MEMacros}{resize=false}{}% 
    \presetkeys{MEMacros}{shrink=false}{}%
    \presetkeys{MEMacros}{tall=false}{}% 
    \presetkeys{MEMacros}{wide=false}{}%
    \presetkeys{MEMacros}{width=3cm}{}%
\makeatother


%%Usage \MESourceFile[keys]{source file}{caption}{label}
\makeatletter
% ========= KEY DEFAULTS =========
\newcommand\MESourceFile[4][]{
\setkeys{MEMacros}{wide=false,language={[ANSI]C},options={}, decorations={},#1}% 
      \begingroup%
        % Set up the listing format, pass language and extra options
    \begingroup\edef\x{\endgroup\noexpand\MESetListingFormat[\ME@options]{\ME@language}}\x
        {\setlength\mywidth{\columnwidth}}
                        {\begin{figure*}[h!btp] }
         \caption{#3}
        \begingroup\edef\x{\endgroup\noexpand\lstinputlisting[label=#4, name=#4,\ME@options] {#2}
            {\ME@decorations} % Decorating comments
            }\x
        {\end{figure*}}
  \endgroup%
}
\makeatother
% % Prepare for some decorations on the listing files
    \makeatletter
    \newif\iflst@linemark

    \lst@AddToHook{EveryLine}{%
 \begingroup
 \advance\c@lstnumber by 1\relax
 \pgfmark{line-\lst@name-\the\c@lstnumber-start}%
 \endgroup
    }

    \lst@AddToHook{EOL}{\pgfmark{line-\lst@name-\the\c@lstnumber-end}%
    \global\lst@linemarktrue
    }

    \lst@AddToHook{OutputBox}{%
 \iflst@linemark
 \pgfmark{line-\lst@name-\the\c@lstnumber-first}%
 \global\lst@linemarkfalse
 \fi
    }

    \def\tkzlst@fnum#1\relax#2\@STOP{%
 \def\@test{#2}%
 \ifx\@test\@empty
 \def\tkzlst@start{0}%
 \else
 \@tempcnta=#1\relax
 \advance\@tempcnta by -1\relax
 \def\tkzlst@start{\the\@tempcnta}%
 \fi
    }

    \lst@AddToHook{Init}{%
 \expandafter\tkzlst@fnum\lst@firstnumber\relax\@STOP
 \pgfmark{line-\lst@name-\tkzlst@start-start}%
    }

% % Put a balloon around some lines in a source
% Usage: \MEHighlightLines{BallonName}{SourceName}{FirstLine}{LastLine}
\newcommand\MEHighlightLines[4]{%
  \pgfmathtruncatemacro\pgf@temp{%
   #3-1
  }%
  \iftikzmark{line-#2-\pgf@temp-start}{%
   \iftikzmark{line-#2-#3-first}{%
     \xdef\b@lines{({pic cs:line-#2-\pgf@temp-start} -| {pic           cs:line-#2-#3-first})}%
   }{%
     \iftikzmark{line-#2-#3-start}{%
       \xdef\b@lines{({pic cs:line-#2-\pgf@temp-start} -| {pic             cs:line-#2-#3-start})}%
     }{%
       \xdef\b@lines{(pic cs:line-#2-\pgf@temp-start)}%
     }%
   }%
  }{%
   \xdef\b@lines{}%
  }%
  \foreach \k in {#3,...,#4} {%
   \iftikzmark{line-#2-\k-first}{%
     \xdef\b@lines{\b@lines (pic cs:line-#2-\k-first) }
   }{}
   \iftikzmark{line-#2-\k-end}{%
     \xdef\b@lines{\b@lines (pic cs:line-#2-\k-end) }
   }{}
  }%
  \ifx\b@lines\pgfutil@empty
  \else
  \edef\pgf@temp{\noexpand\tikz[remember picture,overlay]\noexpand\node[fit={\b@lines}, color=ForestGreen,yshift=-2pt,
     draw, fill=green!30, opacity=0.4,  inner sep=0pt, rounded corners=2pt] (#1) {};
  }%
        \pgf@temp
  \fi
        }
\makeatother

\begin{document}
\MESourceFile[language={[x86masm]Assembler},wide=false
,options={
%\MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}
}
]{HelloWorld.c}{Hello world}{lst:HelloWorld.c}
\MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}
\end{document}
  • I wrote a random C hello world example and the code runs without any errors. The result --> imgur.com/GrpsXa2 EDIT : Oh, after uncommenting I get it. – percusse Mar 23 '15 at 22:29
  • @percusse I simply added both the stand-alone macro and the passed macro, to demonstrate that the problem must be connected to argument passing – katang Mar 24 '15 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.