1

enter image description here

The following is my code, but it didn't work, do I miss some package? Please, give me advice and correct code. Thanks for your help!

\begin{tabular}{|c|c|c|c|}
\hline
& x<0 & x=0 & x>0\\ \hline
y<0 & x^2-y^2 & x^2+y^2 & x^2-y^2\\ \hline
y=0 & x+y & x^2-y^2 & x^2+y^2 & x^2-y^2\\ \hline
y>0 & x^2+y^2 & x+y & x^2+y^2\\ \hline
y<0 & x^2-y^2 & x^2+y^2 & x^2-y^2
\end{tabular}
egreg
  • 1,121,712
Mao
  • 13
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. You might also want to add to your question how you're including the pdfs etc. You can use the array environment – Aradnix Oct 08 '14 at 07:27
  • You have to surround your cells by $...$ as they are mathematical expressions. – LaRiFaRi Oct 08 '14 at 07:28

3 Answers3

2

Given that virtually all of the table's contents should be in math mode, you should use an array environment instead of a tabular environment. You also need to remove the duplicate entry on the y=0 line to make your code compilable.

In order to get a table that doesn't look too much like a bunch of jail cell windows, I'd like to suggest that you also remove all vertical bars as well as most \hlines, and use the macros \toprule, \midrule, and \bottomrule of the booktabs package to obtain well-spaced lines to mark the header row and the end of the table.

enter image description here

\documentclass{article}
\usepackage{booktabs}
\begin{document}
$\begin{array}{cccc}
\toprule
& x<0 & x=0 & x>0\\
\midrule
y<0 & x^2-y^2 & x^2+y^2 & x^2-y^2 \\
y=0 & x+y     & x^2-y^2 & x^2+y^2 \\
y>0 & x^2+y^2 & x+y     & x^2+y^2 \\
y<0 & x^2-y^2 & x^2+y^2 & x^2-y^2 \\
\bottomrule
\end{array}$
\end{document}
Mico
  • 506,678
1

You can use something like this:

\documentclass{article}
\usepackage{amsmath}
\begin{document}

\begin{displaymath}
\begin{array}{|c|c|c|c|}
\hline
    & x<0     & x=0     & x>0\\ \hline
y<0 & x^2-y^2 & x^2+y^2 & x^2-y^2\\ \hline
y=0 & x+y     & x^2-y^2 & x^2+y^2 \\ \hline
y>0 & x^2+y^2 & x+y     & x^2+y^2\\ \hline
y<0 & x^2-y^2 & x^2+y^2 & x^2-y^2 \\ \hline
\end{array}
\end{displaymath}
\end{document}

In this way you can get a kind sort of table in mathematical environment without and $ $ as LaRiFaRi say.s For me is easier in this way.

Aradnix
  • 3,735
  • 2
  • 20
  • 43
0

I reproduced the table, you posted in the image. In your code, you had too many cells. The third row has three & specifiers. This can't work.

You have to put every cell in math-mode. For example with $...$. As you are just using math in the whole table, you can set the complete columns in math mode with help of the package array:

% arara: pdflatex

\documentclass{article}
\usepackage{booktabs,array}

\begin{document}
    \begin{tabular}{>{$}c<{$} >{$}c<{$} >{$}c<{$} >{$}c<{$}}
        \toprule
            & x<0     & x=0     & x>0     \\
        \cmidrule(l){2-4} % the (l) is optional. The screenshot shows a version without it
        y<0 & x^2-y^2 & x^2+y^2 & x^2-y^2 \\
        y=0 & x+y     & x^2-y^2 & x+y     \\
        y>0 & x^2+y^2 & x+y     & x^2+y^2 \\
        \bottomrule
    \end{tabular}
\end{document}

enter image description here

If you need to add more rows with text in it, you will have to write \text{...} in each cell and load the package mathtools for that purpose. You will have to decide for each table if you are having more math or more text and use or omit the >{$}c<{$} likewise.

LaRiFaRi
  • 43,807