如何使用 \foreach 简化代码?

如何使用 \foreach 简化代码?

我需要创建一个动画代码示例演示,因此我决定使用beamer+ minted。这就是我最终想到的:

\documentclass{beamer}

\usepackage{ifthen}
\usepackage{minted}

\newcommand<>{\iterdemo}[3]{%
  \begin{onlyenv}#4
    \begin{columns}
      \begin{column}{0.4\textwidth}
        \ifthenelse{#3 = 1}
        {\inputminted[highlightlines=#1,highlightcolor=structure!50!white]{Delphi}{iterdemo1.pas}}
        {\inputminted[highlightlines=#1,highlightcolor=structure!20!white]{Delphi}{iterdemo1.pas}}
      \end{column}
      \begin{column}{0.4\textwidth}
        \begin{onlyenv}<4-44>
          \ifthenelse{#3 = 2}
          {\inputminted[highlightlines=#2,highlightcolor=structure!50!white]{Delphi}{iterdemo2.pas}}
          {\inputminted[highlightlines=#2,highlightcolor=structure!20!white]{Delphi}{iterdemo2.pas}}
        \end{onlyenv}
      \end{column}
    \end{columns}
  \end{onlyenv}
}

\begin{document}

\begin{frame}
  \iterdemo<1>{0}{0}{1}
  \iterdemo<2>{5}{0}{1}
  \iterdemo<3>{6}{0}{1}
  \iterdemo<4>{6}{0}{1}
  \iterdemo<5>{8}{0}{1}
  \iterdemo<6>{8}{2}{2}
  \iterdemo<7>{8}{3}{2}
  \iterdemo<8>{8}{3}{1}

  \iterdemo<9>{9}{3}{1}
  \iterdemo<10>{10}{3}{1}
  \iterdemo<11>{10}{3}{2}
  \iterdemo<12>{10}{4}{2}
  \iterdemo<13>{10}{4}{1}

  \iterdemo<14>{11}{4}{1}
  \iterdemo<15>{12}{4}{1}
  \iterdemo<16>{12}{4}{2}
  \iterdemo<17>{12}{5}{2}
  \iterdemo<18>{12}{5}{1}

  \iterdemo<19>{13}{5}{1}
  \iterdemo<20>{14}{5}{1}
  \iterdemo<21>{14}{5}{2}
  \iterdemo<22>{14}{6}{2}
  \iterdemo<23>{14}{6}{1}

  \iterdemo<24>{15}{6}{1}
  \iterdemo<25>{16}{6}{1}
  \iterdemo<26>{16}{6}{2}
  \iterdemo<27>{16}{7}{2}
  \iterdemo<28>{16}{7}{1}

  \iterdemo<29>{17}{7}{1}
  \iterdemo<30>{18}{7}{1}
  \iterdemo<31>{18}{7}{2}
  \iterdemo<32>{18}{8}{2}
  \iterdemo<33>{18}{8}{1}

  \iterdemo<34>{19}{8}{1}
  \iterdemo<35>{20}{8}{1}
  \iterdemo<36>{20}{8}{2}
  \iterdemo<37>{20}{9}{2}
  \iterdemo<38>{20}{9}{1}

  \iterdemo<39>{21}{9}{1}
  \iterdemo<40>{22}{9}{1}
  \iterdemo<41>{22}{9}{2}
  \iterdemo<42>{22}{10}{2}
  \iterdemo<43>{22}{0}{1}
  \iterdemo<44>{24}{0}{1}
  \iterdemo<45>{24}{0}{1}
  \iterdemo<46>{26}{0}{1}
\end{frame}

\end{document}

由于大量重复,代码显然不是最佳的(而且创建它很痛苦!)。但是,以下对抗手工劳动的方法对我来说不起作用:

  1. 使用<+->覆盖规范而不是硬编码数字没有效果:内容会出现在每一页上。

  2. 看起来\foreach循环可以轻松删除代码中心部分的大量重复,但我找不到将扩展值作为minted选项传递的方法突出线条。这可能是由于期权pgfkeys的性质所致minted

是否可以简化上述代码,例如通过使用循环?

答案1

我不确定你对 2 有什么想法,但是这个可行......

\documentclass{beamer}

\usepackage{ifthen}
\usepackage{minted}
\usepackage{pgffor}

\newcommand<>{\iterdemo}[3]{%
  \begin{onlyenv}#4
    \begin{columns}
      \begin{column}{0.4\textwidth}
        \ifthenelse{#3 = 1}
        {\inputminted[highlightlines=#1,highlightcolor=structure!50!white]{Delphi}{iterdemo1.pas}}
        {\inputminted[highlightlines=#1,highlightcolor=structure!20!white]{Delphi}{iterdemo1.pas}}
      \end{column}
      \begin{column}{0.4\textwidth}
        \begin{onlyenv}<4-44>
          \ifthenelse{#3 = 2}
          {\inputminted[highlightlines=#2,highlightcolor=structure!50!white]{Delphi}{iterdemo2.pas}}
          {\inputminted[highlightlines=#2,highlightcolor=structure!20!white]{Delphi}{iterdemo2.pas}}
        \end{onlyenv}
      \end{column}
    \end{columns}
  \end{onlyenv}
}

\begin{document}

\begin{frame}
\foreach \x / \y / \z / \a in {1/0/0/1,2/5/0/1,3/6/0/1,4/6/0/1,5/8/0/1,6/8/2/2,7/8/3/2,8/8/3/1,9/9/3/1,10/10/3/1,11/10/3/2,12/10/4/2,13/10/4/1}
{

    \iterdemo<\x>{\y}{\z}{\a}
   }

  \iterdemo<14>{11}{4}{1}
  \iterdemo<15>{12}{4}{1}
  \iterdemo<16>{12}{4}{2}
  \iterdemo<17>{12}{5}{2}
  \iterdemo<18>{12}{5}{1}

  \iterdemo<19>{13}{5}{1}
  \iterdemo<20>{14}{5}{1}
  \iterdemo<21>{14}{5}{2}
  \iterdemo<22>{14}{6}{2}
  \iterdemo<23>{14}{6}{1}

  \iterdemo<24>{15}{6}{1}
  \iterdemo<25>{16}{6}{1}
  \iterdemo<26>{16}{6}{2}
  \iterdemo<27>{16}{7}{2}
  \iterdemo<28>{16}{7}{1}

  \iterdemo<29>{17}{7}{1}
  \iterdemo<30>{18}{7}{1}
  \iterdemo<31>{18}{7}{2}
  \iterdemo<32>{18}{8}{2}
  \iterdemo<33>{18}{8}{1}

  \iterdemo<34>{19}{8}{1}
  \iterdemo<35>{20}{8}{1}
  \iterdemo<36>{20}{8}{2}
  \iterdemo<37>{20}{9}{2}
  \iterdemo<38>{20}{9}{1}

  \iterdemo<39>{21}{9}{1}
  \iterdemo<40>{22}{9}{1}
  \iterdemo<41>{22}{9}{2}
  \iterdemo<42>{22}{10}{2}
  \iterdemo<43>{22}{0}{1}
  \iterdemo<44>{24}{0}{1}
  \iterdemo<45>{24}{0}{1}
  \iterdemo<46>{26}{0}{1}
\end{frame}

\end{document}

相关内容