how to use latexonly for setting tabular column specs

I am trying to compile this example with pdflatex and also with latex2html.

I need to make the table column specification different for each case.

Here is an example of what I have, but this generates an error, and I am not sure how to resolve the error. What is wrong with this?

\documentclass[]{article}%
\usepackage{html}
\begin{document}

\begin{htmlonly}%
\begin{tabular}{|p{.5in}|}
\end{htmlonly}%

\begin{latexonly}%
\begin{tabular}{|c|}
\end{latexonly}%

\hline

test1\\
test2\\

\end{tabular}
\end{document}


then

pdflatex foo.tex


gives

  LaTeX Error: \begin{tabular} on input line 10 ended by \end{latexonly}.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...

l.11 \end{latexonly}
%


Why it thinks tabular ended with latexonly?

I really do not want to make 2 copies of the whole table itself, and write

  \begin{latexonly}.. table ...\end{latexonly}
\begin{htmlonly}.. table ...\end{htmlonly}


I thought I'll try to be 'smart' and just put these around the column specs, but I think I do not understand how Latex environments interact with each others very well.

Unless I am making some silly error, is there a better way to do this (Other than duplicating the whole table)?

thanks, ps. tags here are confusing, which tag should one use for general latex questions? Is this the right group for Latex questions or is the other stackoverflow group the right one?

update I got a solution from another group that works for me, thought to update here.

The solution is to use \latexhtml like this

\latexhtml{% latex version
\begin{tabular}{|c|}%
}{% version for latex2html
\begin{tabular}{|p{.5in}|}%
}


thanks to Robin Fairbairns for the above solution. This worked for my latex2html and pdflatex.

thanks also to everyone here for taking the time to answer. So many things to learn about Latex.

-

You can put the table in a box and then use it as many times as required:

\documentclass{article}
\usepackage{html}

\newsavebox\mytable
\savebox\mytable{%
\begin{tabular}{|p{.5in}|}
\hline
test1\\
test2\\
\hline
\end{tabular}%
}

\begin{document}

\begin{htmlonly}%
\usebox\mytable
\end{htmlonly}%

\begin{latexonly}%
\usebox\mytable
\end{latexonly}%

\end{document}

-
that works with my latex2html and pdflatex, but makes a shaded tables in my latex2html. May be just some setting. But will accept your answer as it worked. Thanks! – Nasser May 17 '12 at 23:29
@NasserM.Abbasi I thought from the original question the aim was to have different preambles in the two cases. If you use a box then the typesetting will be the same in both cases. – David Carlisle May 18 '12 at 8:14

Perhaps something like this:

\documentclass[]{article}%
\usepackage{html}
\begin{document}

\newcommand\tablebody{

\hline

test1\\
test2\\

}

\begin{htmlonly}%
\begin{tabular}{|p{.5in}|}
\tablebody
\end{tabular}
\end{htmlonly}%

\begin{latexonly}%
\begin{tabular}{|c|}
\tablebody
\end{tabular}
\end{latexonly}%

\end{document}

-
thanks David. The above produced the pdf fine, but my latex2html for some reason did not like something. it said %%%%, *** Could not find argument for command \newcommand *** followed by '\tablebody<<4>> then \par then \hline` and more lines like this. It might be a copy paste issue, or something else. – Nasser May 17 '12 at 23:22