![将字符串转换为数字](https://linux22.com/image/270670/%E5%B0%86%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B0%E5%AD%97.png)
我正在尝试编写一个通过表格语法提供(部分)功能并行环境的环境,以便以下两个代码片段能够给出等效的结果:
\begin{Parallel}{4cm}{5cm}
\ParallelLText{TEXT 1}
\ParallelRText{TEXT 2}
\end{ParallelWithTabularSyntax}
(通常语法如上所述)
\begin{ParallelWithTabularSyntax}{p{4cm}p{5cm}}
TEXT 1 & TEXT 2
\end{ParallelWithTabularSyntax}
到目前为止,我已经获得了以下可以运行的代码:
\NewEnviron{ParallelWithTabularSyntax}[1]{%
\StrFindGroup{#1}{1}[\dimL]
\StrFindGroup{#1}{2}[\dimR]
\def \textL {\StrBefore{\BODY}{&}}
\def \textR {\StrBehind{\BODY}{&}}
\begin{Parallel}{0.5\textwidth}{0.5\textwidth}
\ParallelLText{\textL}
\ParallelRText{\textR}
\end{Parallel}
}
但是,我想编写下面的代码来使用用户提供的参数,但这会出现错误!缺少数字,视为零。我猜这是因为字符串 \textL 中的数字被视为字母而不是数字。如何让 LaTeX/TeX 将数字视为数字?
\NewEnviron{ParallelWithTabularSyntax}[1]{%
\StrFindGroup{#1}{1}[\dimL]
\StrFindGroup{#1}{2}[\dimR]
\def \textL {\StrBefore{\BODY}{&}}
\def \textR {\StrBehind{\BODY}{&}}
\begin{Parallel}{\dimL}{\dimR}
\ParallelLText{\textL} % Problematic rows
\ParallelRText{\textR} % ----------------
\end{Parallel}
}
答案1
您的代码存在一些问题。当您这样做时
\StrFindGroup{#1}{1}[\dimL]
\dimL
is{4cm}
和 not的展开4cm
。所以你必须去掉那些括号。提前提取这两个部分也会更有效率。不要忘记展开\BODY
,否则&
将无法扫描。
\documentclass{article}
\usepackage{parallel}
\usepackage{xstring,environ}
\makeatletter
\newcommand{\usedim}[1]{\expandafter\@firstofone#1}
\makeatother
\NewEnviron{ParallelWithTabularSyntax}[1]{%
\StrFindGroup{#1}{1}[\dimL]%
\StrFindGroup{#1}{2}[\dimR]%
\expandarg % <-- you need to expand \BODY or the & isn't scanned
\StrBefore{\BODY}{&}[\textL]%
\StrBehind{\BODY}{&}[\textR]%
\begin{Parallel}{\usedim\dimL}{\usedim\dimR}
\ParallelLText{\textL}%
\ParallelRText{\textR}%
\end{Parallel}
}
\makeatother
\begin{document}
\begin{ParallelWithTabularSyntax}{p{4cm}p{5cm}}
TEXT 1 & TEXT 2
\end{ParallelWithTabularSyntax}
\end{document}
除了使用这个技巧,\usedim
你还可以\begin{Parallel}
使用
\begingroup\edef\x{\endgroup\noexpand\begin{Parallel}\dimL\dimR}\x