2

My scenario is to pass in a ratio (in fractional form as opposed to decimal form) to each table column definition.

The following code cannot be compiled.

\documentclass{article}
\usepackage{pgf}
\usepackage{array}
\usepackage{longtable}

\newcommand{\ratio}[2]%
{%
    \pgfmathsetmacro\temp{(#1)/(#2)}%
    \temp%
}

\newcolumntype{A}[1]%
{%
    >{}%
    m{#1\linewidth-2\tabcolsep-2\arrayrulewidth}%
    <{}%
}

\begin{document}
\ratio{1}{2}
\ratio{2}{5}

\begin{longtable}{|A{\ratio{1}{2}}|A{\ratio{1}{2}}|}\hline
A & B \tabularnewline\hline
A & B \tabularnewline\hline

\end{longtable}
\end{document}
5
  • 1
    Josephs solution works for me (after adding \end{longtable} to the code).
    – Caramdir
    Dec 28 '10 at 11:10
  • It seems to me that your #1\linewidth-2\tabcolsep-2\arrayrulewidth doesn't work. I'm not sure what's happening, but I get a "-2" in the table. Dec 28 '10 at 12:59
  • @Hendrik, you need calc.sty.
    – xport
    Dec 28 '10 at 13:03
  • Then why didn't you include \usepackage{calc} in your code above? I'd suggest you remove the whole piece starting from "Joseph's solution does not work"; his code does work. You just forgot \end{longtable} as the error message says and as Caramdir already noted. Dec 28 '10 at 13:18
  • @Hendrik, I did not read well. Yes. It works :-) We need copy to clipboard button.
    – xport
    Dec 28 '10 at 13:21
7

You cannot use \pgfmathsetratio here, as the arguments to the m box needs to expand to a number. Assuming you can use the e-TeX extensions then you can use an alternative definition:

\makeatletter
\newcommand\ratio[2]{%
  \strip@pt\dimexpr#1pt/#2\relax
}
\makeatother
1
  • thanks for the solution. It works after correcting my own code.
    – xport
    Dec 28 '10 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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