From CIRCA

Jump to: navigation, search

LaTeX code for Tables

Click here for a plain text version of this LaTeX code.
\documentclass[a4paper,11pt]{article}
\usepackage{ulem}
\usepackage{a4wide}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage[pdftex]{graphicx}

\usepackage{hyperref}
% commands generated by html2latex


\begin{document}

Tables may be authored in wiki pages using either HTML table elements directly, or using wikicode formatting to define the table. HTML table elements and their use are well described on various web pages and will not be discussed here. The benefit of wikicode is that the table is constructed of character symbols which tend to make it easier to perceive the table structure in the article editing view compared to HTML table elements.

As a general rule, it is best to avoid using a table unless you need one. Table markup often complicates page editing.
\begin{tabular}

\subsection{Contents}
\begin{itemize}
	\item \hyperlink{Wiki_table_markup_summary}{1Wiki table markup summary}
	\item \hyperlink{Simple_table}{2Simple table}
\begin{itemize}
	\item \hyperlink{Plain}{2.1Plain}
	\item \hyperlink{Alternative}{2.2Alternative}
	\item \hyperlink{With_HTML_attributes}{2.3With HTML attributes}
\begin{itemize}
	\item \hyperlink{border.3D.221.22}{2.3.1border="1"}
	\item \hyperlink{align.3D.22center.22_border.3D.221.22}{2.3.2align="center" border="1"}
	\item \hyperlink{Attributes_on_cells}{2.3.3Attributes on cells}
	\item \hyperlink{Attributes_on_rows}{2.3.4Attributes on rows}
	\item \hyperlink{cellspacing.3D.220.22_border.3D.221.22}{2.3.5cellspacing="0" border="1"}
	\item \hyperlink{cellpadding.3D.2220.22_cellspacing.3D.220.22_border.3D.221.22}{2.3.6cellpadding="20" cellspacing="0" border="1"}
\end{itemize}
	\item \hyperlink{With_HTML_attributes_and_CSS_styles}{2.4With HTML attributes and CSS styles}
\begin{itemize}
	\item \hyperlink{style.3D.22color:green.3Bbackground-color:.23ffffcc.3B.22_cellpadding.3D.2220.22_cellspacing.3D.220.22_border.3D.221.22}{2.4.1style="color:green;background-color:\#ffffcc;" cellpadding="20" cellspacing="0" border="1"}
\end{itemize}
\end{itemize}
	\item \hyperlink{Table_with_TH_headings}{3Table with TH headings}
\begin{itemize}
	\item \hyperlink{Top_headings}{3.1Top headings}
\begin{itemize}
	\item \hyperlink{Each_column}{3.1.1Each column}
	\item \hyperlink{Colspan.3D.222.22}{3.1.2Colspan="2"}
\end{itemize}
	\item \hyperlink{Side_headings}{3.2Side headings}
\begin{itemize}
	\item \hyperlink{Default}{3.2.1Default}
	\item \hyperlink{Right_justify}{3.2.2Right justify}
\end{itemize}
\end{itemize}
	\item \hyperlink{Caption}{4Caption}
	\item \hyperlink{Table_with_H1.2C_H2.2C_H3_etc._headings}{5Table with H1, H2, H3 etc. headings}
\begin{itemize}
	\item \hyperlink{Yummiest}{5.1Yummiest}
\end{itemize}
	\item \hyperlink{Caveat}{6Caveat}
\begin{itemize}
	\item \hyperlink{Negative_numbers}{6.1Negative numbers}
\end{itemize}
\end{itemize}
\end{tabular}\hypertarget{Wiki_table_markup_summary}{}

\subsection{ Wiki table markup summary }
\begin{tabular}\hline 
\{|  & \textbf{table start} \\ 
\hline
|+  &  table \textbf{caption,}\textit{optional;} only between \textbf{table start} and first \textbf{table row} \\ 
\hline
|-  & \textbf{table row,}\textit{optional on first row} -- wiki engine assumes the first row \\ 
\hline
!   & \textbf{table header} cell, \textit{optional.} Consecutive \textbf{table header} cells may be added on same line separated by double marks (!!) or start on new lines, each with its own single mark (!). \\ 
\hline
|   & \textbf{table data} cell, \textit{required!} Consecutive \textbf{table data} cells may be added on same line separated by double marks (||) or start on new lines, each with its own single mark (|). \\ 
\hline
|\}  & \textbf{table end} \\ 
\hline

\end{tabular}
\begin{itemize}
	\item The above marks must \textbf{start on a new line} except the double || and\nolinebreak!! for optionally adding consecutive cells to a line.
	\item \textbf{XHTML attributes.} Each mark, except table end, optionally accepts one or more XHTML attributes. Attributes must be on the same line as the mark. Separate attributes from each other with a single space. 
\begin{itemize}
	\item Cells and caption (| or ||, ! or !!, and |+) hold content. So separate any attributes from content with a single pipe (|). Cell content may follow on same line or on following lines.
	\item Table and row marks (\{| and |-) do not directly hold content. Do \textit{not} add pipe (|) after their optional attributes. If you erroneously add a pipe after attributes for the table mark or row mark the parser will delete it \textit{and} your final attribute if it was touching the erroneous pipe!
\end{itemize}
	\item \textbf{Content} may (a) follow its cell mark on the same line after any optional XHTML attributes or (b) on lines below the cell mark. Content that uses wiki markup that itself needs to start on a new line, such as lists, headers, or nested tables, must of course be on its own new line.
\end{itemize}\hypertarget{Simple_table}{}

\subsection{Simple table}\hypertarget{Plain}{}

\subsubsection{Plain}

The following table lacks borders and good spacing but shows the simplest wiki markup table structure
\begin{tabular}
\begin{tabular} \\ 
 & Orange & Apple \\ 
Bread & Pie \\ 
Butter & Ice cream
\end{tabular}
\begin{verbatim}

{|
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{Alternative}{}

\subsubsection{Alternative}

For more table-ish looking wiki markup cells can be listed on one line separated by ||. This does not scale well for longer cell content such as paragraphs. It works well for short bits of content however, such as our example table.

Extra spaces within cells in the wiki markup can be added, as I have done in the wiki markup below, to make the wiki markup itself look better but they do not affect the actual table rendering.

HTML attributes can be added to tables on this page but have been left out of the following example for simplicity.
\begin{tabular}
\begin{tabular} \\ 
 &   Orange     &    Apple    &    more \\ 
   Bread     &    Pie      &    more \\ 
   Butter    &  Ice cream  &   and more
\end{tabular}
\begin{verbatim}

{|
|  Orange    ||   Apple   ||   more
|-
|   Bread    ||   Pie     ||   more
|-
|   Butter   || Ice cream ||  and more
|}
\end{verbatim}
\end{tabular}\hypertarget{With_HTML_attributes}{}

\subsubsection{With HTML attributes}

You can add HTML attributes to make your table look better\hypertarget{border.3D.221.22}{}

\paragraph{border="1"}
\begin{tabular}
\begin{tabular}\hline 
 & Orange & Apple \\ 
\hline
Bread & Pie \\ 
\hline
Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| border="1"
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{align.3D.22center.22_border.3D.221.22}{}

\paragraph{align="center" border="1"}
\begin{tabular}
\begin{tabular}\hline 
 & Orange & Apple \\ 
\hline
Bread & Pie \\ 
\hline
Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| align="center" border="1"
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{Attributes_on_cells}{}

\paragraph{Attributes on cells }

You can put attributes on individual \textbf{cells.} Numbers for example may look better aligned right
\begin{tabular}
\begin{tabular}\hline 
 & Orange & Apple & 12,333.00 \\ 
\hline
Bread & Pie & 500.00 \\ 
\hline
Butter & Ice cream & 1.00 \\ 
\hline

\end{tabular}
\begin{verbatim}

{| border="1"
|Orange
|Apple
|align="right"|12,333.00
|-
|Bread
|Pie
|align="right"|500.00
|-
|Butter
|Ice cream
|align="right"|1.00
|}
\end{verbatim}
\end{tabular}\hypertarget{Attributes_on_rows}{}

\paragraph{Attributes on rows}

You can put attributes on individual \textbf{rows,} too.
\begin{tabular}
\begin{tabular}\hline 
 & Orange & Apple & 12,333.00 \\ 
\hline
Bread & Pie & 500.00 \\ 
\hline
Butter & Ice cream & 1.00 \\ 
\hline

\end{tabular}
\begin{verbatim}

{| border="1"
|Orange
|Apple
|align="right"|12,333.00
|-
|Bread
|Pie
|align="right"|500.00
|- style="font-style:italic;color:green;"
|Butter
|Ice cream
|align="right"|1.00
|}
\end{verbatim}
\end{tabular}\hypertarget{cellspacing.3D.220.22_border.3D.221.22}{}

\paragraph{cellspacing="0" border="1"}
\begin{tabular}
\begin{tabular}\hline 
 & Orange & Apple \\ 
\hline
Bread & Pie \\ 
\hline
Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| cellspacing="0" border="1"
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{cellpadding.3D.2220.22_cellspacing.3D.220.22_border.3D.221.22}{}

\paragraph{cellpadding="20" cellspacing="0" border="1"}
\begin{tabular}
\begin{tabular}\hline 
 & Orange & Apple \\ 
\hline
Bread & Pie \\ 
\hline
Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| cellpadding="20" cellspacing="0" border="1"
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{With_HTML_attributes_and_CSS_styles}{}

\subsubsection{With HTML attributes and CSS styles}

CSS style attributes can be added with or without other HTML attributes\hypertarget{style.3D.22color:green.3Bbackground-color:.23ffffcc.3B.22_cellpadding.3D.2220.22_cellspacing.3D.220.22_border.3D.221.22}{}

\paragraph{style="color:green;background-color:\#ffffcc;" cellpadding="20" cellspacing="0" border="1"}
\begin{tabular}
\begin{tabular}\hline 
 & Orange & Apple \\ 
\hline
Bread & Pie \\ 
\hline
Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| style="color:green;background-color:#ffffcc;" cellpadding="20" cellspacing="0" border="1"
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{Table_with_TH_headings}{}

\subsection{Table with TH headings}

TH (HTML table headings) can be created by using\nolinebreak! instead of |. Headings usually show up bold and centered by default.\hypertarget{Top_headings}{}

\subsubsection{Top headings}\hypertarget{Each_column}{}

\paragraph{Each column}
\begin{tabular}
\begin{tabular}\hline 
 & \textbf{Yummy} & \textbf{Yummier} \\ 
\hline
Orange & Apple \\ 
\hline
Bread & Pie \\ 
\hline
Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| border="1" cellpadding="20" cellspacing="0"
!Yummy
!Yummier
|-
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{Colspan.3D.222.22}{}

\paragraph{Colspan="2"}
\begin{tabular}
\begin{tabular}\hline 
 & \textbf{Yummies} \\ 
\hline
Orange & Apple \\ 
\hline
Bread & Pie \\ 
\hline
Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| border="1" cellpadding="20" cellspacing="0"
! colspan="2"|Yummies
|-
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{Side_headings}{}

\subsubsection{Side headings}\hypertarget{Default}{}

\paragraph{Default}
\begin{tabular}
\begin{tabular}\hline 
 & \textbf{Fruit} & Orange & Apple \\ 
\hline
\textbf{Dish} & Bread & Pie \\ 
\hline
\textbf{Complement} & Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| border="1" cellpadding="20" cellspacing="0"
!Fruit
|Orange
|Apple
|-
!Dish
|Bread
|Pie
|-
!Complement
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{Right_justify}{}

\paragraph{Right justify}

Right justified side headings can be done as follows
\begin{tabular}
\begin{tabular}\hline 
 & \textbf{Fruit} & Orange & Apple \\ 
\hline
\textbf{Dish} & Bread & Pie \\ 
\hline
\textbf{Complement} & Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| border="1" cellpadding="20" cellspacing="0"
!align="right" |Fruit
|Orange
|Apple
|-
!align="right" |Dish
|Bread
|Pie
|-
!align="right" |Complement
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{Caption}{}

\subsection{Caption}

A \textbf{table caption} can be added to the top of any table as follows
\begin{tabular}
\begin{tabular}\hline 

% if you want to use \caption the tabular must be enclosed in
% table or figure environment
% \caption{Food complements}
 & Orange & Apple \\ 
\hline
Bread & Pie \\ 
\hline
Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| border="1" cellpadding="20" cellspacing="0"
|+Food complements
|-
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}

\textbf{Attributes} can be added to the caption as follows
\begin{tabular}
\begin{tabular}\hline 

% if you want to use \caption the tabular must be enclosed in
% table or figure environment
% \caption{\textit{Food complements}}
 & Orange & Apple \\ 
\hline
Bread & Pie \\ 
\hline
Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| border="1" cellpadding="20" cellspacing="0"
|+align="bottom" style="color:#e76700;"|''Food complements''
|-
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{Table_with_H1.2C_H2.2C_H3_etc._headings}{}

\subsection{Table with H1, H2, H3 etc. headings}

HTML H1, H2, H3, H4 etc. headings can be created the standard wiki markup way with ==equal== signs and \textbf{must be on a line all by themselves} to work.

\textbf{Preview the whole table.} If you click on an edit tab for a heading \textit{within} a table, edit, and preview, the parent table will display erroneously broken because part of it will be missing.

Keep the heading hierarchy consistent with the rest of the page so that the table of contents at page top works correctly.
\begin{tabular}
\begin{tabular}\hline 
 & \hypertarget{Yummiest}{}

\subsubsection{Yummiest} \\ 
\hline
Orange & Apple \\ 
\hline
Bread & Pie \\ 
\hline
Butter & Ice cream \\ 
\hline

\end{tabular}
\begin{verbatim}

{| border="3"  cellpadding="20" cellspacing="0"
|colspan="2"|
===Yummiest===
|-
|Orange
|Apple
|-
|Bread
|Pie
|-
|Butter
|Ice cream 
|}
\end{verbatim}
\end{tabular}\hypertarget{Caveat}{}

\subsection{Caveat}\hypertarget{Negative_numbers}{}

\subsubsection{Negative numbers}

Negative value minus sign can break your table (it may display missing some values) if you start a cell on a new line with a negative number or a parameter that evaluates to a negative number (|-6) because that is the wiki markup for table row, not table cell. To avoid this, insert a space before the value (| -6) or use in-line cell markup (||-6).

\href{/index.php?title=Template:Languages&action=edit&redlink=1}{Template:Languages}

\end{document}
Personal tools