3

I am trying to write an environment which provides (some of) the functionality parallel environment through tabular syntax so that the following two code snippets would give equivalent results:

\begin{Parallel}{4cm}{5cm}
\ParallelLText{TEXT 1}
\ParallelRText{TEXT 2}
\end{ParallelWithTabularSyntax}

(usual syntax above)

\begin{ParallelWithTabularSyntax}{p{4cm}p{5cm}}
TEXT 1 & TEXT 2
\end{ParallelWithTabularSyntax}

So far, I've got the following code, which works:

\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}
}

However, I would like two write the code below to use the arguments given by the user, but this gives the error ! Missing number, treated as zero. I guess the cause for this is that the digits in the string \textL is threated as letters and not as digits. How do you make LaTeX/TeX treat the digits as digits?

\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}
}
2
  • 1
    It isn't that they are treated as letters it is that the \StrBefore isn't an expandable command so it is a sequence of \def and other instructions that would typeset as digits if typesetting but can not be used in an assignment, like \setlength\foo{\def\zz{1pt}\zz} does not work to set \foo to 1pt Jun 20, 2013 at 12:58
  • @DavidCarlisle Can I use some other package instead of xstring to avoid this problem? I.e. is there any expandable alternative to \StrBefore?
    – malin
    Jun 20, 2013 at 13:01

1 Answer 1

3

There are a few issues with your code. When you do

\StrFindGroup{#1}{1}[\dimL]

the expansion of \dimL is {4cm} and not 4cm. So you have to strip off those braces. It's more efficient to extract the two parts in advance, too. Don't forget to expand \BODY or the & wouldn't be scanned.

\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}

In alternative to the trick with \usedim you can change the \begin{Parallel} line with

\begingroup\edef\x{\endgroup\noexpand\begin{Parallel}\dimL\dimR}\x

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .