作为线程的延续https://tex.stackexchange.com/questions/233658/2010-2014-versions-difference:
我正在尝试用突出显示的行、编号球等来装饰程序列表。在 2010 年,我使用了一个宏,该宏不再适用于 2014 TeX (OSX)。在代码中,我确实有一个宏调用
\MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}
它应该突出显示源代码清单的 1-3 行。正如 MWE 中给出的那样,它起作用了(尽管我认为定位有所改变)。我想将此宏作为“装饰”传递给列表宏,以便将其放在图形环境中。我在宏中使用它作为
\begingroup\edef\x{\endgroup\noexpand\lstinputlisting[label=#4, name=#4,\ME@options] {#2}
{\ME@decorations} % Decorating comments
}\x
并将其作为
\MESourceFile[language={[x86masm]Assembler},wide=false
,options={
%\MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}
}
当然,没有注释。我猜这种方式和直接调用是一样的。然而,我收到了
! Undefined control sequence.
\pgfmathsetcount ... \pgfmath@onquick #2\pgfmath@
{\afterassignment \pgfmath...
l.192 ...loWorld.c}{Hello world}{lst:HelloWorld.c}
怎么了?
\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}