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.

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}
share|improve this question
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. –  Hendrik Vogt 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. –  Hendrik Vogt 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
add comment

1 Answer

up vote 6 down vote accepted

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
share|improve this answer
    
thanks for the solution. It works after correcting my own code. –  xport Dec 28 '10 at 13:29
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.