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.

There has got to be a better/easier way to do this!! And by the way, how does one format the code [when posting a question] to look nice, compact, and full of colours?

\documentclass{article}

\usepackage[2.5cm]{geometry}% http://ctan.org/pkg/geometry
\usepackage{amsmath}% http://ctan.org/pkg/amsmath

\begin{document}
\begin{tabular}{ l || l c c c c c c c r |}  
  $x$ \vspace{1pt}  
  & $ -\, \infty $  
  & \hspace*{-0.5cm}   
  & \hspace*{-0.5cm} $ -\, 1 $  
  & \hspace*{-0.5cm}  
  & \hspace*{-0.7cm} $0$  
  & \hspace*{-1.0cm}  
  & \hspace*{-0.5cm} $+\, 1$  
  & \hspace*{-0.5cm}  
  & \hspace*{-0.5cm} $ +\, \infty $ \\  
\hline

  \parbox[t][1.0cm]{2.0cm}{\vspace{1pt} $y' \,=\, \dfrac{\,x^{2} \,-\, 1\,}{x^{2}}$ \vspace{1pt}}  
  & $\begin{array}{c} \\ + \quad +\\ \end{array}$  
  & \hspace*{-0.5cm} $\begin{array}{c} \\ + \quad + \quad +  \\ \end{array}$  
  & \hspace*{-0.5cm} $\begin{array}{c} \\ 0 \\ \end{array}$  
  & \hspace*{-0.5cm} $\begin{array}{c} \\ - \quad - \quad - \\ \end{array}$  
  & \hspace*{-0.7cm} $\begin{array}{c} \\ \vert \\ \end{array}$  
  & \hspace*{-1.0cm} $\begin{array}{c} \\ - \quad - \quad - \\ \end{array}$  
  & \hspace*{-0.5cm} $\begin{array}{c} \\ 0 \\ \end{array}$  
  & \hspace*{-0.5cm} $\begin{array}{c} \\ + \quad + \quad + \\ \end{array}$  
  & \hspace*{-1.0cm} $\begin{array}{c} \\ + \\ \end{array}$ \\  
\hline 

  \parbox[t][1.0cm]{2.0cm}{\vspace{1pt} $y'' \,=\, \dfrac{2}{\,x^{3}\,}$ \vspace{1pt}}  
  & $\begin{array}{c} \\ - \quad - \\ \end{array}$  
  & \hspace*{-0.5cm} $\begin{array}{c} \\ - \quad - \quad - \\ \end{array}$  
  & \hspace*{-0.5cm} $\begin{array}{c} \\ - \\ \end{array}$  
  & \hspace*{-0.5cm} $\begin{array}{c} \\ - \quad - \quad - \\ \end{array}$  
  & \hspace*{-0.7cm} $\begin{array}{c} \\ \vert \\ \end{array}$  
  & \hspace*{-1.0cm} $\begin{array}{c} \\ + \quad + \quad + \\ \end{array}$  
  & \hspace*{-0.5cm} $\begin{array}{c} \\ + \\ \end{array}$  
  & \hspace*{-0.5cm} $\begin{array}{c} \\ + \quad + \quad + \\ \end{array}$  
  & \hspace*{-1.0cm} $\begin{array}{c} \\ + \\ \end{array}$ \\  
\hline 

  \parbox[t][1.0cm]{2.0cm}{ $y \,=\, x \,+\, \dfrac{1}{\,x\,}$ }  
  & $-\, \infty \;\; \nearrow$  
  & \hspace*{-0.5cm} $ \begin{array}{c} \\ \nearrow \quad \nearrow \quad \nearrow \\ \bigcap \end{array} $  
  & \hspace*{-0.5cm} $ -\, 2 $  
  & \hspace*{-0.5cm} $ \begin{array}{c} \\ \searrow \quad \searrow \quad \searrow \\ \bigcap \end{array} $  
  & \hspace*{-0.7cm} $ \;\genfrac{}{}{0.0pt}{}{}{}_{-\,\infty} {\Big |}^{+\, \infty}\; $  
  & \hspace*{-0.7cm} $ \begin{array}{c} \\ \searrow \quad \searrow \quad \searrow \\ \bigcup \end{array} $  
  & \hspace*{-0.5cm} $ +\, 2 $  
  & \hspace*{-0.5cm} $ \begin{array}{c} \\ \nearrow \quad \nearrow \quad \nearrow \\ \bigcup \end{array} \vspace{2pt} $  
  & \hspace*{-1.0cm} $+\, \infty$ \\  
\hline  

\end{tabular}\\
\end{document}
share|improve this question
    
You format code by tabbing 4 spaces or use the button {}. Best is to provide a minimum working example. –  Yiannis Lazarides Dec 20 '11 at 4:02
1  
Possibly related: tex.stackexchange.com/a/30629/2552 –  Jake Dec 20 '11 at 4:23
1  
Just out of curiosity, where did you get this from? Was it a WYSIWYG? –  cmhughes Dec 20 '11 at 4:26
1  
@Diegis: I think cmhughes means the code you used to generate the table. It looks like it was created with some program, and not written by hand, because there is so much repetition in it. Is that the case? –  Jake Dec 20 '11 at 5:03
2  
:-) Are you kiddin' me??? I wrote all that by hand, almost 10 hours of fiddling and no idea if I was doing the right thing! I had to format it that way otherwise I couldn't figure out what needed to be aligned under what, and that bar with the infinity signs by the side took me half a day to bring under control -- all of it is too manual and error prone, I wish it was a smarter way to do these tables, one table per year is OK, but if you have more each week, it's murder... This is why I posted it, gotta be a smarter and faster way of doing these kind of tables... Or not? :-( –  Diegis Dec 20 '11 at 5:44
show 3 more comments

3 Answers

In my original comment, I asked if your code came from a WYSIWYG, because it looked as if it had a lot of unnecessary code- typically you get this kind of code when (attempting) to export from another program to TeX.

Anyway, here's my version of your table:

screenshot

In the code below I have

  • deleted all of the \begin{array}{c}...\end{array}
  • deleted all of the \hspace*{...}
  • loaded the booktabs package which gives lots of useful features for tables, including \toprule, \midrule, and \bottomrule
  • loaded the tabularx package to allow the table to stretch to (in this case) the \textwidth
  • made the row separation larger by changing \arraystretch

You'll also notice that I have been quite sparing with my +, -, \searrow, and \nearrow. As the examples get more complicated and you need to consider more intervals, you would necessarily need to delete a lot of them anyway.

If you want to see how wide the table is, you can change showframe=true in the loading of the geometry package. If ever you want your table to be wider than the standard textwidth, then you can enclose it in an adjustwidth environment from the changepage package.

\documentclass{article}

\usepackage[showframe=false]{geometry}
\usepackage{booktabs}
\usepackage{amsmath}
\usepackage{tabularx}

% use this package if you want to temporarily 
% widen the page
%\usepackage{changepage}

% use this columntype if you want to RIGHT justify your columns
\newcolumntype{Y}{>{\raggedleft\arraybackslash}X}


\begin{document}

\renewcommand{\arraystretch}{3}

\noindent\begin{tabularx}{\textwidth}{p{2cm}XXXXXXXXX}
 \toprule
    $x$                         &       $-\infty$   &                  &  $-1$ &                   &   $0$                      &                  &   $+1$  &                   &   $\infty$    \\ \midrule
    $y'=\dfrac{x^2-1}{x^2}$     &       $+$         &      $+$         &  $0$  &       $-$         &   $|$                      &   $-$            &   $0$   &      $+$          &   $+$         \\
    $y''=\dfrac{2}{x^3}$        &       $-$         &      $-$         &  $-$  &       $-$         &   $|$                      &   $+$            &   $+$   &      $+$          &   $+$         \\
    $y=x+\dfrac{1}{x}$          &       $-\infty$   & $\nearrow\bigcap$&  $-2$ & $\searrow\bigcap$ &   ${}_{-\infty}|^{+\infty}$&$\searrow\bigcup$ &   $+2$  & $\nearrow\bigcup$ &   $\infty$\\
 \bottomrule
\end{tabularx}



\end{document}
share|improve this answer
add comment

You are probably looking for the tablor package. Its documentation is in French, but it's rich in examples (the example code is available, on TeX Live, by texdoc tablor.html).

There is also a TikZ based package, tkz-tab, also with many example (and French documentation).

share|improve this answer
    
If you use google chrome to open tablor.html then you will get an option to translate it to English. –  Sony Dec 23 '11 at 16:31
add comment

Here's an attempt to simplify cmhughes answer further by dropping down to more primitive level (no packages, every column is defined mathmode in the alignment preamble, and a couple of macros to shorten input):

\documentclass{minimal}
\def\myalign#1{% should be used inside displayed equation
  \def\trule{\noalign{\medskip\hrule\medskip}}
  \def\nebc{\nearrow\bigcup}
  \def\sebc{\searrow\bigcup}
  \def\pminf{{}_{-\infty}|^{+\infty}}
  \let\Inf\infty
  % ^make a couple of macros to shorten the input (scoped inside this macro)
  \vbox{% enclose inside a vbox so it will not be broken across pages
  \openup2\jot% increase interlineskips by a multiple of symbolic unit of
              % measure (jot default is 3pt) for consistency
  \mathsurround0pt
  \tabskip1em plus\displaywidth % tabskip glue gets inserted between columns
  % with "optimal" value of 1em, but with stretchability to displaywidth.
  \halign to\displaywidth{% try to stretch the table to accomodate the whole
                          % equation display width
  &$\displaystyle\hfil##\hfil$\crcr
  % ^alignment preamble; the definition gets repeated for every column by the
  % first ampersand. The column content will be centered (\hfil##\hfil)
  #1% user input
  \crcr}}}
\begin{document}
\[
\myalign{
x                   & -\Inf &       & -1 &       & 0      &       & +1 &       & \Inf \cr
\trule
y'={x^2-1\over x^2} & +     & +     & 0  & -     & |      & -     & 0  & +     & + \cr
y''={2\over x^3}    & -     & -     & -  & -     & |      & +     & +  & +     & + \cr
y=x+{1\over x}      & -\Inf & \nebc & -2 & \sebc & \pminf & \sebc & +2 & \nebc & \Inf \cr
}
\]
\end{document}

Caveats:

  • I've seen people say primitive use is discouraged (I'm not sure why, though)
  • There is surplus tabskip glues at the beginning and end (I don't think it matters much when the column content is centered, though).

Edit: As per comment, to make it look more similar, it'd need the extra rules, left-align the columns and to drop the surplus tabskip glues. But just to make some differences, I chose not to make it look exactly the same:

\documentclass{article}
\def\myalign#1{%
  \def\trule{\noalign{\smallskip\hrule\medskip}}
  \def\nebc{\nearrow\bigcup}
  \def\sebc{\searrow\bigcup}
  \def\pminf{{}_{-\infty}|^{+\infty}}
  \let\Inf\infty
  \def\amp{&}% props to Bruno; I just love this trick
  \vbox{\mathsurround0pt\openup1\jot
    \halign{%
      &$\displaystyle##\hfil\tabskip0pt$&\amp##\tabskip1em\crcr
      \noalign{\hrule height1pt\smallskip}#1\noalign{\smallskip\hrule height1pt}\crcr}}}
\begin{document}
\[
\myalign{
x                   & -\Inf &       & -1 &       & 0      &       & +1 &       & \Inf \cr
\trule
y'={x^2-1\over x^2} & +     & +     & 0  & -     & |      & -     & 0  & +     & + \cr
y''={2\over x^3}    & -     & -     & -  & -     & |      & +     & +  & +     & + \cr
y=x+{1\over x}      & -\Inf & \nebc & -2 & \sebc & \pminf & \sebc & +2 & \nebc & \Inf \cr
}
\]
\end{document}

Which looks like:
enter image description here

It drops the second caveat of the first version, but introduces another: because it is essentially doubling the number of columns (the \amp in the preamble), it affects commands which are used to span multiple columns.

Now, I'd like to remind that to retain document wide consistency you'd want to keep all those skips and rules and whatnots the same. Packages like booktabs help you achieve that easily.

share|improve this answer
    
Nothing against going to low level commands; however you should say \tabskip=1em plus\displaywidth (to get real centering) before the \halign and also \mathsurround=0pt, so as not to repeat the assignment over and over again. –  egreg Dec 21 '11 at 10:17
    
@egreg: true, fixed. –  morbusg Dec 21 '11 at 10:35
    
This is truly awesome, I have never seen LaTeX code like this! Somehow it reminds me of programming in C, so dense and compact but at the same time so powerful! I admit I have to study this example line by line, most of its syntax is alien to me, so thank you all for a great learning opportunity!! I will try and work with this example and get the same aspect as the previous one, I like the simplicity of this example, but I am curious to see how this example can be brought to look the same as the first one, but with the low level commands alone. Thank you again, Colin. –  Diegis Dec 22 '11 at 20:46
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.