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.

I'm trying to make a little summary of mathematic analysis, useful for my university, in LaTeX. I'm stuck in the problem of creating nice table, with alternate colouring containing equations with various alignments.

I succeeded in building such a table only with displaymath inside a \parbox, which doesn't allow equation alignments, but not with align*.

Here is my code:

\documentclass[a4paper]{article}
\usepackage{multicol}
\usepackage[table]{xcolor}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{lipsum}
\begin{document}
    \begin{multicols}{2}
    \lipsum[1-3]
    % Row color alternating
    \rowcolors{1}{gray!25}{white}
    \begin{tabular}{c}
        \hline
        \parbox{\linewidth}{$$f : \mathbb{R} \to \mathbb{Q}$$}\\
        \parbox{\linewidth}{$$g : \mathbb{Z} \to \mathbb{C}$$}\\
        \parbox{\linewidth}{$$h : \mathbb{R} \to \mathbb{Q}$$}\\
        $f(g(x)) = \frac{\pi}{2}$\\

        % This doesn't work
        %\begin{align*}&f(g(x)) = \frac{\pi}{2}&&\forall x > 0\end{align*}\\
        \hline
    \end{tabular}
    \lipsum[4-6]
    \end{multicols}
\end{document}
share|improve this question

migrated from stackoverflow.com Feb 17 '12 at 4:19

This question came from our site for professional and enthusiast programmers.

    
See Why is \[...\] preferable to $$? for some advice on math notation and use. –  Werner Feb 17 '12 at 4:40
    
Tnx a lot, very useful, I'm pretty new in latex –  ColOfAbRiX Feb 17 '12 at 13:41

2 Answers 2

up vote 9 down vote accepted

There are perhaps many ways of doing this. However, requiring a table structure with alternating row colours limits your possibilities. Here is a mock-up of your current document with some alterations:

enter image description here

\documentclass[a4paper]{article}
\usepackage[margin=1in]{geometry}% http://ctan.org/pkg/geometry
\usepackage{multicol}% http://ctan.org/pkg/multicol
\usepackage{array}% http://ctan.org/pkg/array
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{amsmath}% http://ctan.org/pkg/amsmath
\usepackage{amsfonts}% http://ctan.org/pkg/amsfonts
\usepackage{lipsum}% http://ctan.org/pkg/lipsum
\renewcommand{\arraystretch}{1.5}% Increase the row height of tabular/array
\newcolumntype{C}{>{\centering\arraybackslash$}p{\linewidth}<{$}}
\begin{document}
  \begin{multicols}{2}
    \lipsum[1-5] \par \medskip

    % Row color alternating
    \rowcolors{1}{gray!25}{white}%
    \setlength{\tabcolsep}{0pt}%
    \noindent\begin{tabular}{C}
      \hline
      f : \mathbb{R} \to \mathbb{Q} \\
      g : \mathbb{Z} \to \mathbb{C} \\
      h : \mathbb{R} \to \mathbb{Q} \\
      f(g(x)) = \frac{\pi}{2}\phantom{\ \forall x > 0} \\
      f(g(x)) = \frac{\pi}{2}\ \forall x > 0 \\
      \hline
    \end{tabular} \par \medskip

    \lipsum[6-8]
  \end{multicols}
\end{document}

Here are some comments pertaining to the adjusted example:

  • I've used geometry to adjust the margins, since the default margin setup in two-column mode I find too narrow;
  • The table is set apart from the flowing text using \par\medskip to introduce a medium vertical skip as a separation;
  • Since the tabular is an element (in horizontal mode) on its own, it starts a paragraph and is therefore indented by \parindent. Using \noindent removes this indentation, setting is flush left with the column margin;
  • A single column of width \linewidth makes the tabular stretch the entire width of the column (\columnwidth would have worked just as well);
  • Left- and right-column padding is removed via \setlength{\tabcolsep}{0pt};
  • tabular row height is increased by a factor of 50% via \renewcommand{\arraystretch}{1.5};
  • Although the single tabular is set in paragraph style (p{\linewidth}), the array package column specification allows for inserting <stuff> before each column entry using >{<stuff>} and appending afterwards using <{<stuff>}. Therefore,

    \newcolumntype{C}{>{\centering\arraybackslash$}p{\linewidth}<{$}}
    

    defines a new column type C that centres its contents (\centering) in math mode $...$.

  • Horizontal alignment with other row elements is possible via ample use of \phantom{...}, since using align environments do not work across the tabular rows.
share|improve this answer
    
First thanks, there are a lot of things I didn't know, I improved and cleaned my pages! I modified the column type definition without centering and without inline math \newcolumntype{C}{ >{\arraybackslash$\displaystyle} p{\linewidth} <{$} } and the row height \renewcommand{\arraystretch}{2.5}. But now the eqns are aligned to the bottom, they should be aligned in the middle of the row. I did a quick search with no luck... –  ColOfAbRiX Feb 18 '12 at 15:30

I would rather forget about stripes, they are really meant to be left to zebras and not mathematical books and they do not enhance readability as is sometimes claimed (see link in comments). If you want to have a bit more of a modern look and similar to what you want, use the excellent tcolorbox package by Prof. Thomas F. Sturm, author of
LATEX – Einführung in das Textsatzsystem   who also now kindly provided documentation in English.

enter image description here

Here is a MWE:

\documentclass[twocolumn]{article}
\usepackage[listings,theorems]{tcolorbox}
\tcbset{before={\par\medskip\pagebreak[0]\noindent},after={\par\medskip}}%
\begin{document}
\section{Table of Equations}
\newcounter{texercise}[section]
\begin{tcolorbox}[colback=blue!5,colframe=blue!50!black,arc=0mm,
theorem={Equation}{texercise}{Summation}{myMarker}]{Summation of Numbers}{summation}
  For all natural number $n$ it holds:\\[2mm]
  \[\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}\]
\end{tcolorbox}
\begin{tcolorbox}[colback=blue!5,colframe=blue!50!black,arc=0mm,
theorem={Equations}{texercise}{More on summation}{myMarker}]{Summation of Numbers}{summation}
  For all natural number $n$ it holds:\\[2mm]
   \[\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}\]
  \[\displaystyle\sum\limits_{i=1}^n i = \frac{n(n+1)}{2}+ \frac{n(n+1)}{2}\]
\end{tcolorbox}
\end{document}

You can include any material in the boxes, provided by tcolorbox, including tabulars.

enter image description here

\begin{tcolorbox}[colback=blue!5,colframe=blue!50!black,arc=0mm,
theorem={Equations}{texercise}{More on summation}{myMarker}]{Summation of Numbers}{summation}
\medskip

\begin{tabular}{lr@{~}c@{~}l@{\qquad}r}
\multicolumn{5}{l}{\hspace{-2ex}\textbf{Line Values.}} \\
\multicolumn{5}{r}{}\\
\multicolumn{4}{l}{Altitude of triangle on side $a$,} \\
& $h$ &=& \( \displaystyle \frac{2}{a}
             \sqrt{s(s-a)(s-b)(s-c)} \) & \\
%
\multicolumn{4}{l}{Median of triangle on side $a$,} \\
& $m$ &=& \( \frac{1}{2} \sqrt{2(b^2+c^2) - a^2} \) & \ \\
\multicolumn{5}{l}{\hspace{-2ex}\textbf{Areas.}} \\
%
Rectangle,     & $S$ &=& $b\times h$ &  \\
Square,         & $S$ &=& $b^2$        &  \\
\end{tabular}
\end{tcolorbox}

The package has many options and one needs to study the documentation carefully. The English documentation has only been recently added, so you might need to update your distribution.

share|improve this answer
    
That's very interesting, I'll take a deep look. But I think zebras are better because I want to compile a table of notable integrals, with lot of rows, and I think alternating colour should improve readability. –  ColOfAbRiX Feb 17 '12 at 13:56
    
@ColOfAbRiX Zebras and readability alistapart.com/d/zebrastripingdoesithelp/…, but nature and the army knew all that long ago, as zebras use zebras for camouflage:) –  Yiannis Lazarides Feb 18 '12 at 9:03

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.