9

I am looking for ideas how to make TeX understand that when # appears both in the if-else and in the else-fi part of a conditional in an alignment tab, in the end there will never be more than one # in the tab. Here is some sample code:

\def\table{
   \halign{
      \iftrue\hfil##\else##\hfil\fi\cr
      Bla-bla\cr}}
\table

4 Answers 4

9

You can pretend to have two # in the template by using a macro:

\newif\ifright
\def\table{%
  \halign{%
    \def\temp{##}\ifright\hfil\temp\else\temp\hfil\fi\cr
    Bla-bla\cr
    aaaaaaaaaaaaaaaaaaa\cr
  }%
}

% \rightfalse
\table

\righttrue
\table

\bye

enter image description here

Alternatively, use \span

\newif\ifright
\def\table{%
  \halign{%
    \span\ifright\hfil##\span\else##\hfil\span\fi\cr
    Bla-bla\cr
    aaaaaaaaaaaaaaaaaaa\cr
  }%
}

% \rightfalse
\table

\righttrue
\table

\bye
4
  • How about not using \newif and \span: \halign{\def\hash{#}\iftrue\hfil\hash\else\hash\hfil\fi\cr Bla-bla\cr}
    – SJU
    Oct 16, 2014 at 18:27
  • Or maybe even better (no extra macros): \halign{\span\iftrue\hfil#\span\else#\hfil\span\fi\cr Bla-bla\cr}
    – SJU
    Oct 16, 2014 at 18:38
  • @AngelTsankov Of course; but I thought you had a purpose in having \halign inside a macro.
    – egreg
    Oct 16, 2014 at 19:24
  • Yes, I meant to have \halign inside a macro. However, I cannot get the point of \newif\ifright and \def\temp{##} when just \span seems to suffice (both when \halign is in a macro and when it is not).
    – SJU
    Oct 17, 2014 at 12:51
6

Are you looking for something like the following

\let\hash\relax
\def\table#1{%
  \edef\preamble{\iftrue\hfil\hash\else\hash\fi\cr}%
  \let\hash##
  \expandafter\halign\expandafter{\preamble #1}%
  \let\hash\relax
}
\table{a\cr b\cr}
\bye

The idea here is that during the 'construction' phase we represent # using a non-expandable token; only during the actual table construction is \hash equal to #. In this way there is never a conditional containing multiple # tokens.

3
  • The LaTeX kernel's approach for creating the tabular environment uses a similar idea, with the name \@sharp for # (see \@mkpream and so on).
    – Joseph Wright
    Oct 16, 2014 at 15:41
  • What is the purpose of \let\hash\relax at the beginning?
    – SJU
    Oct 16, 2014 at 15:52
  • @AngelTsankov Try without and see :-) The \edef will expand everything it can, and if \hash is undefined you'll get an error.
    – Joseph Wright
    Oct 16, 2014 at 15:53
6

As an alternative to Joseph's you can hide the duplication in a macro (at the expense of passing each cell through a macro expansion)

\def\foo#1{\iftrue\hfil#1\else#1\hfil\fi}
\def\table{%
   \halign{\foo{##}\cr
      Bla-bla\cr}}
\table

\bye
2

I'am using toks register \tabdata for such purposes where the \halign preamble is created step by step. The advatage of this is that the macro \table can create arbitrary \halign preamble according to special language offered to user. The algorithm can scan such language and it can create \halign preamble using \addtabdata macro.

\newif\ifright
\newtoks\tabdata

\def\table{\tabdata={}% the \tabdata idea inspired from OPmac
   \ifright \addtabdata{\hfil##}\else\addtabdata{##\hfil}\fi
   \halign\expandafter{\the\tabdata\cr
      ddghsghdfs\cr fghdg\cr}
}
\def\addtabdata#1{\tabdata=\expandafter{\the\tabdata#1}}

\table

\righttrue
\table

\end

You must log in to answer this question.

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