13

I currently have a Plain TeX file which does something like this in a multitude of places:

\indent\hbox{\vbox{\halign{#\hfil\quad&#\hfil\cr
... table content here ...
}}}

I would love to abstract things out so that this could instead read as:

\mySpecialHAlign{
... table content here ...
}

However, such a macro is not easy for a relative newbie such as myself to define, for reasons that will be obvious to anybody who gives it a try.

Is this sort of macro technically possible to create with the right sort of TeX incantations, or am I doomed to repeat the whole hbox-vbox-halign preamble each and every time?

(Note again that I'm using Plain TeX)

3
  • 1
    Welcome to TeX.SX!
    – mafp
    Jun 22, 2013 at 16:55
  • 1
    May I asked why do you work with plain TeX? I think a beginner should start with LaTeX. Jun 22, 2013 at 17:00
  • 3
    When I started out with TeX as a kid the first book I came across was Knuth's TeXbook. I got a copy and gave it a read. I hardly even knew LaTeX existed at the time, and so Plain TeX sort of just stuck. It's never given me any problems... save for people in chatroom and on forums always asking me why I use it ;-) Jun 22, 2013 at 18:03

2 Answers 2

16
\long\def\mytable#1{%
  \leavevmode
  \vbox{%
    \halign{%
      ##\hfil\quad&##\hfil\cr
      #1\crcr
    }%
  }%
}

\mytable{a&b\cr cc&dd\cr}% or
\mytable{a&b\cr cc&dd}

Remarks:

  • \long: The argument of \mytable may use paragraphs (\par).
  • I do not think, it belongs to a table macro to decide, whether the current paragraph starts with an indent, if the table macro is used as start of a paragraph. IMHO, \leavevmode is the better choice and it is also used in LaTeX.
  • The additional \hbox serves no purpose, thus I have removed it in the macro.
  • \crcr helps to catch the case, the user forgets the final \cr. \crcr does nothing, if it is following a \cr or \noalign{...}, but it becomes \cr otherwise to close the final table row.
  • # in \halign needs doubling inside definitions.

Variant without argument:

It is also possible to avoid the argument. This is useful, if the arguemnt might contain verbatim code that requires catcode changes:

\def\BeginTable{%
  \leavevmode
  \vbox\bgroup
    \halign\bgroup
      ##\hfil\quad&##\hfil\cr
}
\def\EndTable{%
      \crcr
    \egroup
  \egroup
}

\BeginTable a&b\cr cc&dd\cr\EndTable % or
\BeginTable a&b\cr cc&dd\EndTable
1
  • Ah! \bgroup! Wonderful! Yes, when I started thinking about how to write this macro I essentially wanted to do something like: \def\startTable{\halign{}, and then \def\endTable{}}, which of course wouldn't work. I was suspecting something like \bgroup existed, but I wasn't sure where to look for it. Thanks a million for pointing it out. Jun 22, 2013 at 18:12
3

Try

\def\mySpecialHAlign#1{\indent\hbox{\vbox{\halign{##\hfil\quad&##\hfil\cr #1 }}}}

Note the doubling of # since the definition is inside a macro.

You must log in to answer this question.

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