如何在 Plain TeX 中创建局部变量?

如何在 Plain TeX 中创建局部变量?

我想通过创建一个局部变量作为临时容器来保存的值来简化下面的代码\numexpr#1+#2+#3+#4+#5}

\documentclass[dvips,dvipsnames,rgb,table]{book}
\usepackage[a4paper,hmargin=20mm,vmargin=20mm]{geometry}
\usepackage{longtable}
\usepackage{array}
\usepackage{calc}
\usepackage{lscape}

\usepackage{pstricks,pst-node}
\newpsstyle{gridstyle}{%
gridwidth=0.4pt,%default: 0.8pt
gridcolor=Red!20,%default: black
griddots=0,%default: 0 
%
gridlabels=3pt,%default: 10pt
gridlabelcolor=Blue,%default: black
%
subgriddiv=5,%default: 5
subgridwidth=0.2pt,%default: 0.4pt
subgridcolor=Green!20,%default: gray
subgriddots=0%default: 0
}



\makeatletter
\newcommand\ratio[2]{\strip@pt\dimexpr#1pt/#2\relax}
\makeatother



\setlength{\tabcolsep}{8pt}
\setlength{\arrayrulewidth}{0.5pt}
\arrayrulecolor{Red}


\newcounter{No}
\renewcommand{\theNo}{\arabic{No}}


\newcolumntype{S}[1]%
{%
    >{\begin{minipage}{#1\linewidth-2\tabcolsep-2\arrayrulewidth}\vspace{\tabcolsep}}%
  c%
  <{\vspace{\tabcolsep}\end{minipage}}%
}%
\newcolumntype{O}[1]%
{%
    >{\begin{minipage}{#1\linewidth-2\tabcolsep-1.5\arrayrulewidth}\vspace{\tabcolsep}}%
  c%
  <{\vspace{\tabcolsep}\end{minipage}}%
}%
\newcolumntype{I}[1]%
{%
    >{\begin{minipage}{#1\linewidth-2\tabcolsep-\arrayrulewidth}\vspace{\tabcolsep}}%
  c%
  <{\vspace{\tabcolsep}\end{minipage}}%
}%


\newenvironment{InlineTable}[5][0]%
{%    
    \setcounter{No}{0}%
    \newcount\ITT
    \ITT\numexpr#1+#2+#3+#4+#5\relax
    \begin{longtable}%
    {%
            |>{\stepcounter{No}\centering\scriptsize\theNo}O{\ratio{#2}{\ITT}}<{}%
            |>{\centering}I{\ratio{#3}{\ITT}}<{\input{\jobname.tmp}}%
            |>{\centering\lstinputlisting{\jobname.tmp}}I{\ratio{#4}{\ITT}}<{}%
            |>{\scriptsize}O{\ratio{#5}{\ITT}}<{}%
            |%
    }%
    \hline\ignorespaces%
}%
{%
    \end{longtable}%
}


\newcommand{\Comment}[1]{&&&#1\tabularnewline\hline}

\usepackage{listings}
\lstset{%
language={PSTricks},
alsolanguage={[LaTeX]TeX},
breaklines=true,
basicstyle=\ttfamily\scriptsize,%
keywordstyle=\color{blue},
backgroundcolor=\color{yellow!30}%
}
\usepackage{fancyvrb}


\def\MyRow{%        
        \VerbatimEnvironment%
        \begin{VerbatimOut}{\jobname.tmp}%
}

\def\endMyRow{%
        \end{VerbatimOut}%      
}




\usepackage{lipsum}



\begin{document}
\clearpage
\pagestyle{empty}
%Landscape starts here.
\begin{landscape}

\begin{InlineTable}[10]{5}{25}{30}{40}%
%=============
\begin{MyRow}
\pspicture*[showgrid](3,3)
\pnode(1,1){A}
\pnode(3,3){B}
\ncline{A}{B}
\endpspicture
\end{MyRow}
\Comment{\lipsum[1]}
%=============
\begin{MyRow}
\begin{pspicture}[showgrid](3,3)
\psframe*[linecolor=red!30](3,2)
\end{pspicture}
\end{MyRow}
\Comment{\lipsum[2]}
%=============
\begin{MyRow}
\pspicture[showgrid](3,3)
\psframe*[linecolor=green!30](3,2)
\endpspicture
\end{MyRow}
\Comment{\lipsum[3]}
%=============
\begin{MyRow}
\pspicture[showgrid](3,3)
\psframe*[linecolor=Yellow](3,2)
\endpspicture
\end{MyRow}
\Comment{%
\begin{equation}
\int_a^b f(x)\, \textrm{d}x=F(b)-F(a)
\end{equation}
is the fundamental theorem of calculus.
}
%=============
\begin{MyRow}
\pspicture[showgrid](3,3)
\psframe*[linecolor=Maroon!30](3,2)
\endpspicture
\end{MyRow}
\Comment{%
Today I ate burnt bread. It was so delicious.
\begin{equation}
\int_a^b f(x)\, \textrm{d}x=F(b)-F(a)
\end{equation}
is the fundamental theorem of calculus.
}
%=============
\end{InlineTable}
\end{landscape}
%Landscape stops here.
\pagestyle{plain}
\end{document}

答案1

LaTeX 环境形成组,因此您在其中设置的任何变量都将是本地变量,除非您使用\global。现在,LaTeX 计数器是全局设置的,因此您需要一个 TeX 计数:

\newcount\mycount
...
\newenvironment{InlineTable}[5][0]%
{%
  \mycount\numexpr#1+#2+#3+#4+#5\relax
...

相关内容