使用 keyval 强制宏扩展

使用 keyval 强制宏扩展

在尝试回答这个问题,我遇到了一个似乎无法克服的障碍。该listings包(与许多其他包一样)用于keyval处理配置。如果该配置的一部分是必须事先展开的宏,该怎么办?

所有(不可否认是半随机的)配置都会\expandafter产生某种错误。我遇到的最接近的错误是收到一个错误,抱怨 TeX 在 之前遇到了文本(键)\begin{document}

有什么办法可以解决这个问题吗?

平均能量损失

\documentclass[a4paper, 12pt,english]{article}
\usepackage{color,forloop}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkgreen}{rgb}{0.0,0.4,0.0}
\definecolor{darkred}{rgb}{0.6,0.0,0.0}

\newcounter{ct}
\newcommand{\lstkeygenr}{%
  \forloop{ct}{0}{\value{ct} < 50}{%
    r\arabic{ct},
  }
}

\usepackage{listings}
\lstdefinelanguage{mylanguage}
                  {keywords=[1]{add,sub,test},
                   keywords=[2]{r0,r1,r2,\lstkeygenr},
                   keywords=[3]{p00,p01,p10,p11},
                   moredelim=[s][\footnotesize\textit]{<}{>},
                   keywordstyle=[1]\normalsize\color{gray}\bfseries\ttfamily,
                   keywordstyle=[2]\color{red},
                   keywordstyle=[3]\color{darkred}}

\begin{document}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,language=mylanguage}
\begin{lstlisting}
  p00 add r0 , r1 , r2 , r3 , r4
\end{lstlisting}

\end{document}

答案1

您不想传递以循环作为键值的宏,您想传递由这样的循环生成的列表,因此您需要反转您的调用顺序:

在此处输入图片描述

\documentclass[a4paper, 12pt,english]{article}
\usepackage{color,forloop}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkgreen}{rgb}{0.0,0.4,0.0}
\definecolor{darkred}{rgb}{0.6,0.0,0.0}

\newcounter{ct}
\makeatletter
\def\rlist{\@gobble}
\forloop{ct}{0}{\value{ct} < 50}{%
    \edef\rlist{\rlist,r\arabic{ct}}
  }

\makeatother
\def\listwithrs#1\relax{%
\lstdefinelanguage{mylanguage}
                  {keywords=[1]{add,sub,test},
                   keywords=[2]{#1},
                   keywords=[3]{p00,p01,p10,p11},
                   moredelim=[s][\footnotesize\textit]{<}{>},
                   keywordstyle=[1]\normalsize\color{gray}\bfseries\ttfamily,
                   keywordstyle=[2]\color{red},
                   keywordstyle=[3]\color{darkred}}}

\usepackage{listings}
\expandafter\listwithrs\rlist\relax

\begin{document}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,language=mylanguage}
\begin{lstlisting}
  p00 add r0 , r1 , r2 , r3 , r4
\end{lstlisting}

\end{document}

根据要求,定义 xlist 而不是 rlist 的版本采用定义前缀字母的宏(因此\xlist r与原始版本相同\rlist

\documentclass[a4paper, 12pt,english]{article}
\usepackage{color,forloop}
\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkgreen}{rgb}{0.0,0.4,0.0}
\definecolor{darkred}{rgb}{0.6,0.0,0.0}

\newcounter{ct}
\makeatletter
\def\xxlist{\@gobble}
\let\xhash\relax
\forloop{ct}{0}{\value{ct} < 50}{%
    \edef\xxlist{\xxlist,\xhash1\arabic{ct}}
  }
\let\xhash#
\expandafter\def\expandafter\xlist\expandafter#\expandafter1\expandafter{\xxlist}

\typeout{^^J\string\xlist^^J\meaning\xlist}

\makeatother
\def\listwithrs#1\relax{%
\lstdefinelanguage{mylanguage}
                  {keywords=[1]{add,sub,test},
                   keywords=[2]{#1},
                   keywords=[3]{p00,p01,p10,p11},
                   moredelim=[s][\footnotesize\textit]{<}{>},
                   keywordstyle=[1]\normalsize\color{gray}\bfseries\ttfamily,
                   keywordstyle=[2]\color{red},
                   keywordstyle=[3]\color{darkred}}}

\usepackage{listings}

\expandafter\listwithrs\xlist r\relax

\begin{document}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1, numbersep=5pt,language=mylanguage}
\begin{lstlisting}
  p00 add r0 , r1 , r2 , r3 , r4
\end{lstlisting}

\end{document}

相关内容