7

I need to copy a table from a pdf file to my LaTeX document. I know I have to do it by hand but still don't understand how to create a table with LaTeX.

I need to translate this table back to its LaTeX commands

abdu
  • 1,849

2 Answers2

14

A simple version of the table:

\documentclass[12pt]{article}

\renewcommand*{\arraystretch}{1.2}

\begin{document}
\noindent
\begin{tabular}{|c|*{12}{c}}
  \hline
  Alloy & Ni & Cr & Co & Mo & W & Al & Ti & Ta & Re & Nb & V & Hf \\
  \hline
  \multicolumn{13}{|l}{First generation}\\
  \hline
  PWA 1480  & 62.5 & 10  & 5   &     & 4   & 5   & 1.5 & 12 \\
  Ren\'e N4 & 62.6 & 9   & 8   & 2   & 6   & 3.7 & 4.2 & 4   &   & 0.5 \\
  CMSX-2    & 66.6 & 8   & 4.6 & 0.6 & 7.9 & 5.6 & 0.9 & 5.8 \\
  SRR 99    & 66.5 & 8.5 & 5   &     & 9.5 & 5.5 & 2.2 & 2.8 \\
  \hline
  \multicolumn{13}{|l}{Second generation}\\
  \hline
  PWA 1484  & 59.4 & 5   & 10  & 2   & 6   & 5.6 &     & 9   & 3 \\
  Ren\'e N5 & 61.8 & 7   & 8   & 2   & 5   & 6.2 &     & 7   & 3 &     & & 0.2 \\
\end{tabular}
\end{document}

(The missing follwing lines of the table are left as exercise for the reader.)

Result

Finetuning:

  • *{12}{c} is a shortcut for cccccccccccc.

  • In tabulars the line distances are managed by struts with a height and depth that sums up to a text line. With horizontal lines, the text might be too near to the lines, thus \renewcommand*{\arraystretch}{1.2} increases thesse struts by 20 percent.

  • Alternative is using the length \extrarowheight, This amount is added to a row height in a table, e.g.:

    \usepackage{array}
    \setlength{\extrarowheight}{.2ex}
    
  • If you want to align the numbers at the dot, then take a look at packages dcolumn or siunitx.

Moriambar
  • 11,466
Heiko Oberdiek
  • 271,626
14

Here, the figure labels are printed as Table x.y where x is the section number and y is a table number within section x.

\documentclass{article}

\usepackage[hmargin=3cm,vmargin=3.75cm]{geometry} % Here, only used to get rid of a overfull \hbox warning.
\usepackage{booktabs,dcolumn}                     % Tables.
\usepackage[figureposition=bottom]{caption}       % Float captions.

% Globally set table float position.
\makeatletter
  \providecommand*\setfloatlocations[2]{\@namedef{fps@#1}{#2}}
\makeatother
\setfloatlocations{table}{htbp}

% Redefine table counter.
\makeatletter
  \@addtoreset{table}{section}
\makeatother
\renewcommand*\thetable{\arabic{section}.\arabic{table}}

% Simplify notation.
\newcolumntype{d}[1]{D{.}{.}{#1}}
\newcommand*\mc[1]{\multicolumn{1}{c}{#1}}
\newcommand*\mcTwo[1]{\multicolumn{2}{l}{#1}}

\begin{document}

\section{First section}
Something.

\begin{table}
\centering
 \caption{A data table.}
 \label{tbl:1}
  \begin{tabular}{c c}
   A & B \\
   C & D
  \end{tabular}
\end{table}

\begin{table}
\centering
 \caption{A data table.}
 \label{tbl:2}
  \begin{tabular}{c c}
   E & F \\
   G & H
  \end{tabular}
\end{table}

\section{Second section}
Something.

\begin{table}
\centering
 \caption{A data table.}
 \label{tbl:3}
  \begin{tabular}{c d{2.1} d{2.1} d{2.1} d{1.1} d{1.1} d{1.2} d{1.1} d{1.1} d{1.1} d{1.1} d{1.0} d{1.2}}
   \toprule
    Alloy & \mc{Ni} & \mc{Cr} & \mc{Co} & \mc{Mo} & \mc{W} & \mc{Al} & \mc{Ti} & \mc{Ta} & \mc{Re} & \mc{Nb} & \mc{V} & \mc{Hf} \\
   \midrule
    \mcTwo{First generation} & & & & & & & & & & & \\
   \midrule
    PWA~1480    & 62.5 & 10   &  5   & \mc{---} & 4        & 5    & 1.5      & 12   & \mc{---} & \mc{---} & \mc{---} & \mc{---} \\[1ex]
    Ren{\'e}~N4 & 62.6 &  9   &  8   & 2        & 6        & 3.7  & 4.2      &  4   & \mc{---} & 0.5 & \mc{---} & \mc{---}      \\[1ex]
    CMSX-2      & 66.6 &  8   &  4.6 & 0.6      & 7.9      & 5.6  & 0.9      &  5.8 & \mc{---} & \mc{---} & \mc{---} & \mc{---} \\[1ex]
    SRR~99      & 66.5 &  8.5 &  5   & \mc{---} & 9.5      & 5.5  & 2.2      &  2.8 & \mc{---} & \mc{---} & \mc{---} & \mc{---} \\
   \midrule
    \mcTwo{Second generation} & & & & & & & & & & & \\
   \midrule
    PWA~1484    & 59.4 &  5   & 10   & 2        & 6        & 5.6  & \mc{---} &  9   & 3        & \mc{---} & \mc{---} & \mc{---} \\[1ex]
    Ren{\'e}~N5 & 61.8 &  7   &  8   & 2        & 5        & 6.2  & \mc{---} &  7   & 3        & 0.5      & \mc{---} & 0.2      \\[1ex]
    CMSX~4      & 61.8 &  6.5 &  9   & 0.6      & 6        & 5.6  & 1        &  6.5 & 3        & \mc{---} & \mc{---} & 0.1      \\[1ex]
    CMSX~6      & 70.4 & 10   &  5   & 3        & \mc{---} & 4.8  & 4.7      &  2   & \mc{---} & \mc{---} & \mc{---} & 0.1      \\
   \midrule
    \mcTwo{Third generation} & & & & & & & & & & & \\
   \midrule
    CMSX-10     & 69.6 &  2   &  3   & 0.4      & 5        & 5.7  & 0.2      &  8   & 6        & 0.1      & \mc{---} & 0.03     \\[1ex]
    Ren{\'e}~N6 & 57.4 &  4.2 & 12.5 & 1.4      & 6        & 5.75 & 0        &  7.2 & 5.4      & 0        & 0        & 0.15     \\
   \bottomrule
  \end{tabular}
\end{table}

\end{document}

output

Update

Another way of creating the table is using the S column from siunitx;

\documentclass{article}

\usepackage[
%  locale = DE % if you want a comma as decimal separator instead of a dot
]{siunitx}
%\usepackage[version = 3]{mhchem} % typesetting chemical formulae using \ce{}
\usepackage{booktabs}

\newcommand*\mcTwo[1]{\multicolumn{2}{l}{#1}}

\begin{document}

\begin{tabular}{
  c
  *{3}{S[table-format = 2.1]}
  *{2}{S[table-format = 1.1]}
       S[table-format = 1.2]
  *{4}{S[table-format = 1.1]}
       S[table-format = 1]
       S[table-format = 1.2]
}
 \toprule
  Alloy       & {Ni} & {Cr} & {Co} & {Mo}  & {W}   & {Al} & {Ti}  & {Ta} & {Re}  & {Nb}  & {V}   & {Hf}  \\
 \midrule
  \mcTwo{First generation} & & & & & & & & & & & \\
 \midrule
  PWA~1480    & 62.5 & 10   &  5   & {---} & 4     & 5    & 1.5   & 12   & {---} & {---} & {---} & {---} \\[1ex]
  Ren{\'e}~N4 & 62.6 &  9   &  8   & 2     & 6     & 3.7  & 4.2   &  4   & {---} & 0.5   & {---} & {---} \\[1ex]
  CMSX-2      & 66.6 &  8   &  4.6 & 0.6   & 7.9   & 5.6  & 0.9   &  5.8 & {---} & {---} & {---} & {---} \\[1ex]
  SRR~99      & 66.5 &  8.5 &  5   & {---} & 9.5   & 5.5  & 2.2   &  2.8 & {---} & {---} & {---} & {---} \\
 \midrule
  \mcTwo{Second generation} & & & & & & & & & & & \\
 \midrule
  PWA~1484    & 59.4 &  5   & 10   & 2     & 6     & 5.6  & {---} &  9   & 3     & {---} & {---} & {---} \\[1ex]
  Ren{\'e}~N5 & 61.8 &  7   &  8   & 2     & 5     & 6.2  & {---} &  7   & 3     & 0.5   & {---} & 0.2   \\[1ex]
  CMSX~4      & 61.8 &  6.5 &  9   & 0.6   & 6     & 5.6  & 1     &  6.5 & 3     & {---} & {---} & 0.1   \\[1ex]
  CMSX~6      & 70.4 & 10   &  5   & 3     & {---} & 4.8  & 4.7   &  2   & {---} & {---} & {---} & 0.1   \\
 \midrule
  \mcTwo{Third generation} & & & & & & & & & & & \\
 \midrule
  CMSX-10     & 69.6 &  2   &  3   & 0.4   & 5     & 5.7  & 0.2   &  8   & 6     & 0.1   & {---} & 0.03  \\[1ex]
  Ren{\'e}~N6 & 57.4 &  4.2 & 12.5 & 1.4   & 6     & 5.75 & 0     &  7.2 & 5.4   & 0     & 0     & 0.15  \\
 \bottomrule
\end{tabular}

\end{document}

(Personally, I prefer this method.)

  • thank you Heiko for you suggestion. To be honest I've never created a table before with LaTeX and these whole commands are like chinese for me. I copied pasted your code Svend with all the packages but there is a missing file the terminal says: 'mhchem.sty' not found I understood through Heiko's code many elementary things but still can't create a table myself. – abdu May 20 '13 at 20:10
  • @abdu This means that the package is not installed (correctly) on your system. – Svend Tveskæg May 20 '13 at 20:12
  • I'm using ubuntu and I've been trying to install the package but it failed. Why do we need this package? – abdu May 20 '13 at 20:21
  • @abdu I used it to typeset the names of the chemical elemtents correct. (If you remove the package and change \mc{\ce{name}} to \mc{name}, you get the same output but the other appreach is more rigorous.) – Svend Tveskæg May 20 '13 at 20:27
  • Svend you're a big help :) I removed the mhchem package and it works now. But I would really like to understand every single command I've been using LaTeX for about 3 weeks and I think I still need too much experience to be able to do the things you do. Do you have any suggestions what to read? Or do you have some pages with which you learned creating tables? – abdu May 20 '13 at 20:35
  • @abdu For mathematics, Math mode by Herbert Voss is a good start. Also the LaTeX entry at Wikibooks is good. (I myself used a big manual in Danish by Lars Madsen aka. daleif when i started learning LaTeX.) – Svend Tveskæg May 20 '13 at 20:44
  • 2
    @abdu Start by exploring the site. For example: What is the best book to start learning LaTeX? – Alan Munn May 20 '13 at 20:45
  • @SvendTveskæg and AlanMunn thank you! I still have a short and a last question concerning the label and the caption of the table. For example in the code you posted Svend. I need to change it to Tabelle 2.1 and if possible in bold. By the way, can I find your manual in German? – abdu May 20 '13 at 20:54
  • @abdu You should start a new question I think. – Svend Tveskæg May 20 '13 at 20:57
  • @SvendTveskæg a new question about the caption? I just need to know how to remove the automatic labelling. I'll then be able to type it manually in the caption. That's all. Thanks! – abdu May 20 '13 at 21:07
  • @abdu To manually typeset a label like this is a bad thing; let LaTeX do it for you. (See my updated answer.) – Svend Tveskæg May 21 '13 at 08:08
  • @SvendTveskæg I understand what you mean and you're right if I'm matching my table to the chapter and section but that's not my case.

    \begin{document} \chapter*{Kapitel 1} \section*{Einleitung} \subsection*{1.1 Motivation}

    Chapter's name is actually Chapter and the section should have no numbering. So I have to do the typesetting manually. Is there a way?

    – abdu May 21 '13 at 18:05
  • @abdu I don't know. – Svend Tveskæg May 21 '13 at 18:07
  • @SvendTveskæg The last edit made your answer CW; avoid minor corrections. – egreg Dec 26 '13 at 15:46