tikz/pgf 升级和装饰

tikz/pgf 升级和装饰

自从 pgf 进行了重大升级后,我的代码就无法再编译了。我试图制作“装饰”源列表,其中的装饰可以是突出显示某些行(参见 MWE)、某些行上的球、图像等。装饰是可选的,我将它们作为宏参数传递。这是我的 MWE

    \documentclass{book}
    \usepackage{xcolor}
    \usepackage{xkeyval}
    \usepackage{listings}
    \usepackage{tikz}   
\usetikzlibrary{shapes,positioning,arrows,fit,backgrounds,calc,tikzmark,shadows}%

    \definecolor{ForestGreen}{rgb}{0.0, 0.4, 0.0}
    \definecolor{burntorange}{rgb}{0.8, 0.33, 0.0}
    \newlength{\mywidth} % Used in defining listing width
    \definecolor{ivory}{rgb}{1.0, 1.0, 0.94}
        \colorlet{SeparatorColor}{burntorange}

        \makeatletter
        \define@key{MEMacros}{decorations}{\def\ME@decorations{#1}}
        \define@key{MEMacros}{language}{\def\ME@language{#1}}
        \define@key{MEMacros}{number}{\def\ME@number{#1}}
        \presetkeys{MEMacros}{number=1}{}%
    \makeatother


    %%Usage \MESourceFile[keys]{source file}{caption}{label}
    \makeatletter
    \newcommand\MESourceFile[4][]{
    \setkeys{MEMacros}{ %language={[ANSI]C},
     decorations={},#1}% 
          \begingroup%
            \lstset{    language={[ANSI]C}      }   

             {\begin{figure*}[h!btp] }
             \caption{#3}
            \begingroup\edef\x{\endgroup\noexpand\lstinputlisting[label=#4, name=#4] {#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=1pt, rounded corners=5pt] (#1) {};
      }%
            \pgf@temp
      \fi
            }
    \makeatother

    \begin{document}
    \MESourceFile[decorations={
    %\MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}
    }
    ]{HelloWorld.c}{Hello world}{lst:HelloWorld.c}
    \MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}
    \end{document}

以及我想要装饰的伟大代码

#include <stdio.h>

int main()
{
     printf("hello, world\n");
}

代码的目的是将 1-3 行周围突出显示。代码按原样工作。这只是为了证明 \MEHighlightLines 宏工作正常。现在您可以用该宏注释掉倒数第二行,并取消注释上面 3 行包含相同宏的行。

现在您收到错误消息

! Undefined control sequence.
\pgfmathsetcount ... \pgfmath@onquick #2\pgfmath@ 
                                                  {\afterassignment \pgfmath...

由于唯一的区别是现在宏作为参数传递,而不是直接调用,我猜原因隐藏在新 pgf 中扩展宏。参数传递方法是

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

错误发生在升级后的 pgf 中扩展我传递的宏的某个地方。(装饰必须与列表一起保存,因此我强制将它们放在 \figure 中。也欢迎其他实现想法(作为一种解决方法),但最好的方法是修复宏扩展错误。)

答案1

好吧,错误很明显:代码

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

应该

\lstinputlisting[label=#4, name=#4] {#2}
\ME@decorations % Decorating comments

这里完全没有理由使用这个\edef\x技巧,问题恰恰在于它\ME@decorations无法生存,\edef因为它包含作业(例如\pgfmathtruncatemacro)。

顺便说一句,也是{\begin{figure*}[htbp]}错误{\end{figure*}}的,\lstset可以很好地追随\begin{figure*},这样周围的一切\begingroup\endgroup变得多余了。

这是一个修复(并重新格式化)的版本。

\documentclass{book}
\usepackage{xcolor}
\usepackage{xkeyval}
\usepackage{listings}
\usepackage{tikz}   
\usetikzlibrary{
  shapes,
  positioning,
  arrows,
  fit,
  backgrounds,
  calc,
  tikzmark,
  shadows,
}

\definecolor{ForestGreen}{rgb}{0.0, 0.4, 0.0}
\definecolor{burntorange}{rgb}{0.8, 0.33, 0.0}
\newlength{\mywidth} % Used in defining listing width
\definecolor{ivory}{rgb}{1.0, 1.0, 0.94}
\colorlet{SeparatorColor}{burntorange}

\makeatletter
\define@key{MEMacros}{decorations}{\def\ME@decorations{#1}}
\define@key{MEMacros}{language}{\def\ME@language{#1}}
\define@key{MEMacros}{number}{\def\ME@number{#1}}
\presetkeys{MEMacros}{number=1}{}%
\makeatother

%%Usage \MESourceFile[keys]{source file}{caption}{label}
\makeatletter
\newcommand\MESourceFile[4][]{%
  \setkeys{MEMacros}{%
    %language={[ANSI]C},%
    decorations={},%
    #1%
  }%
  \begin{figure*}[h!btp]
  \lstset{language={[ANSI]C}}
  \caption{#3}
  \lstinputlisting[label=#4, name=#4]{#2}%
  \ME@decorations % Decorating comments
  \end{figure*}
}
%% Prepare for some decorations on the listing files
\newif\iflst@linemark

\lst@AddToHook{EveryLine}{%
  \begingroup
  \advance\c@lstnumber by \@ne
  \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 \m@ne
    \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=1pt, rounded corners=5pt
      ] (#1) {};
    }%
    \pgf@temp
  \fi
}
\makeatother

\begin{document}

\MESourceFile[%
  decorations={
    \MEHighlightLines{HelloWorldsystem}{lst:HelloWorld.c}{1}{3}
  }%
]{HelloWorld.c}{Hello world}{lst:HelloWorld.c}

\end{document}

在此处输入图片描述

相关内容