Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

When I used >{$\sqrt\bgroup\tfrac}c<{\egroup$}, I got the following output. The weird part happened at the last column. How to fix it?

enter image description here

\documentclass[border=3pt]{standalone}
\usepackage{booktabs}
\usepackage{array}
\usepackage{amsmath}

\begin{document}

\begin{tabular}
{
    >{$}c<{^\circ$}
    >{$\tfrac1 2\sqrt\bgroup}c<{\egroup$}
    >{$\tfrac1 2\sqrt\bgroup}c<{\egroup$}
    >{$\sqrt\bgroup\tfrac}c<{\egroup$}
}
\toprule
\multicolumn{1}{c}{$\theta$}&
    \multicolumn{1}{c}{$\sin\theta$}&
        \multicolumn{1}{c}{$\cos\theta$}&
            \multicolumn{1}{c}{$\tan\theta$}\\
\midrule
0&
    0&
        4&
            0 4\\
30&
    1&
        3&
            1 3\\
45&
    2&
        2&
            2 2\\
60&
    3&
        1&
            3 1\\
90&
    4&
        0&
            4 0\\
\bottomrule
\end{tabular}

\end{document}

Bonus: Please make the rows a bit taller. And if it is possible, can we extract the second and third column to be used for the fourth column?

share|improve this question
add comment

2 Answers

up vote 10 down vote accepted

You can not use \bgroup to enclose macro arguments.

As noted in comments \sqrt doesn't as it happen take its argument as a macro argument.

The last column is different because & is a TeX primitive which causes the text from <{...} to be inserted as soon as it is seen, however \\ is a macro and macros are not expanded while scanning for arguments. So you would need to use a dummy last column that is always empty so your scan always ends with & or (with some loss of functionality) use \cr primitive rather than \\. Or (best) insert \\ rather than \endgroup into the preamble with <{\\} and in the >{...} don't use \sqrt directly but use a macro using delimited arguments \foo#1\\ this delimited argument will then always pick up the content (and you have to put a real \ back if you are in the last row)


enter image description here

\documentclass[border=3pt]{standalone}
\usepackage{booktabs}
\usepackage{array}
\usepackage{amsmath}

\setlength\extrarowheight{2pt}

\def\getNum#1\relax{\gdef\num{#1}$\tfrac{1}{2}\sqrt{#1}$}

\def\getDenum#1\\{\gdef\denum{#1}$\tfrac{1}{2}\sqrt{#1}$&$\sqrt{\tfrac{\num}{\denum}}$\\}

\begin{document}

\begin{tabular}
{
    >{$}c<{^\circ$}
    >{\getNum}c
    >{\getDenum}c
    c
}
\toprule
\multicolumn{1}{c}{$\theta$}&
    \multicolumn{1}{c}{$\sin\theta$}&
        \multicolumn{1}{c}{$\cos\theta$}&
            \multicolumn{1}{c}{$\tan\theta$}\\
\midrule
0&
    0&
        4\\
30&
    1&
        3\\
45&
    2&
        2\\
60&
    3&
        1\\
90&
    4&
        0\\
\bottomrule
\end{tabular}

\end{document}
share|improve this answer
    
I think it's not true what you're writing. The \sqrt is defined as a \radical, and a \radical takes a <math field> as "argument". A <math field> can be {<math mode material>}, where {} are explicitly allowed to be \bgroup and \egroup. For details see the TeXbook, page 291. –  Hendrik Vogt Jul 19 '12 at 14:08
    
By the way, the OP's code has the effect that the \tfrac in the last column takes \ignorespaces as its first argument. Unfortunately I can't tell why this is happening. –  Hendrik Vogt Jul 19 '12 at 14:11
    
@HendrikVogt yes although \DeclareRobustCommand\sqrt{\@ifnextchar[\@sqrt\sqrtsign} use of bgroup in that way isn't supported in latex even if it accidentally works (but I'll edit the text a bit) –  David Carlisle Jul 19 '12 at 14:16
    
Well, LaTeX uses TeX and nothing else. \sqrtsign is defined as \radical "270370\relax (almost the same as TeX's \sqrt), so what I wrote about \radical still applies. (But yes, you're right, what I wrote above was an oversimplification.) –  Hendrik Vogt Jul 19 '12 at 14:17
    
@HendrikVogt yes I was agreeing with your comment:-) –  David Carlisle Jul 19 '12 at 14:20
show 1 more comment

The collcell package by Martin Scharrer solves the problem:

\documentclass[border=3pt]{standalone}
\usepackage{booktabs}
\usepackage{array}
\usepackage{amsmath}
\usepackage{collcell}

\newcommand\getnumerator[1]{$\tfrac{1}{2}\sqrt{#1}$\gdef\numerator{#1}}
\newcommand\getdenominator[1]{$\tfrac{1}{2}\sqrt{#1}$\gdef\denominator{#1}}

\begin{document}

\begin{tabular}
  {
   >{$}c<{^\circ$}
   >{\collectcell\getnumerator}c<{\endcollectcell}
   >{\collectcell\getdenominator}c<{\endcollectcell}
   >{$\sqrt{\tfrac{\numerator}{\denominator}}$}c
  }
\toprule
\multicolumn{1}{c}{$\theta$}&
\multicolumn{1}{c}{$\sin\theta$}&
\multicolumn{1}{c}{$\cos\theta$}&
\multicolumn{1}{c}{$\tan\theta$}\\
\midrule
0  & 0 & 4 & \\\addlinespace
30 & 1 & 3 & \\\addlinespace
45 & 2 & 2 & \\\addlinespace
60 & 3 & 1 & \\\addlinespace
90 & 4 & 0 & \\
\bottomrule
\end{tabular}

\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.