17

I am working on a book that has many many such tables. Is there a way to create shortcut for the code shown below. I want to make table caption a variable, because in the MWE shown below except \caption and cell data everything else is constant.

what I am trying to achieve is - I should be able to say \starttable ..... \endtable copying this much code will give me the results I want but it will make it less like LaTeX

\begin{document}

\begin{table}[!htbp]
\begin{minipage}[b]{0.5\linewidth}%\centering
{\caption{Table name - 1}
\renewcommand{\arraystretch}{1.1}
\begin{tabular}{|c|c|c|c|}

\hline
Text-1  & Text-2 & Text-3 & Text-4 \\
 \hline
   &  &  &  \\
  &   &   &   \\
&   &   &   \\
\hline
\end{tabular}}

\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.5\linewidth}
\centering
{\caption{Table Name - 2}
\renewcommand{\arraystretch}{1.1}
\begin{tabular}{|c|c|c|c|}
\hline
Text-1  & Text-2 & Text-3 & Text-4 \\
 \hline
   &  &  &  \\
  &   &   &   \\
&   &   &   \\
\hline
\end{tabular}}

\end{minipage}
\end{table}
3

2 Answers 2

16

I can understand you desire to simplify capturing of tabular material and to keep the mark-up clean.

Normally, what I do for complicated tables is firstly to simplify the row entries. In your case we will mark the table data as follows:

\starttable
 \R test|test|test|test\\
 \R test|test|test|test\\
 \R test|test|test|test\\
\stoptable

I find using an "|" is easier to the eye than an "&" and the marked up text looks more tidy. The \starttable and \stoptable are just two macros to hold the head and end materials.

\documentclass{article}
\begin{document}
\def\starttable{%
  \begin{table}[!htbp]
  \renewcommand{\arraystretch}{1.1}
  \begin{tabular}{|c|c|c|c|}
  \hline
  Text-1  & Text-2 & Text-3 & Text-4 \\
 \hline}
\def\stoptable{\hline\end{tabular}\end{table}}
\def\R #1|#2|#3|#4{ #1&#2&#3&#4}

 \starttable
 \R test|test|test|test\\
 \R test|test|test|test\\
 \R test|test|test|test\\
\stoptable
\starttable %badly marked up table rather use the "|"
   &  &  &  \\
  &   &   &   \\
&   &   &   \\
\stoptable
\end{document}

Captions and other styling information can be added by modifying the \startmacro to take a parameter.

\def\starttable#1{%
\begin{table}[!htbp]
\caption{#1}
...

I am not sure if you always have "double tables". In such a case I will rewrite the above in a different set of macros, such as \starttwotables and \endtwotables. Please note that LaTeX will not allow you to define a macro starting with an \end unless it is an environment.

Edit for multi-tables

You can add one or more tables with same syntax as above. Enclose in \begin{table}..., if you need them to float or leave it out for immediate placement.

enter image description here

\documentclass{article}
\makeatletter
\usepackage[figurename=Figure.,
                  justification=RaggedRight, 
                  labelfont={bf, footnotesize}, 
                  textfont={footnotesize},position=top]{caption}
\begin{document}
\listoftables
\def\starttable#1{%
  \renewcommand{\arraystretch}{1.1}%
  \minipage{0.45\textwidth}
      \captionof{table}{#1}
      \tabular{|c|c|c|c|}
      \hline
      Text-1  & Text-2 & Text-3 & Text-4 \\
      \hline
}
\def\stoptable{%
   \hline\endtabular
   \endminipage\hspace{10pt}}
\def\R #1|#2|#3|#4{ #1&#2&#3&#4}

\newpage
\begin{table}[h]
\centering
\starttable{First table caption}
 \R test|test|test|test\\
 \R test|test|test|test\\
 \R test|test|test|test\\
\stoptable
%
\starttable{This is the second caption}
 \R test|test|test|test\\
 \R test|test|test|test\\
 \R test|test|test|test\\
\stoptable
\bigskip

\starttable{This is the caption}
 \R test|test|test|test\\
 \R test|test|test|test\\
 \R test|test|test|test\\
\stoptable
%
\starttable{This is the caption}
 \R test|test|test|test\\
 \R test|test|test|test\\
 \R test|test|test|test\\
\stoptable

\starttable{This is the caption}
 \R test|test|test|test\\
 \R test|test|test|test\\
 \R test|test|test|test\\
\stoptable
\end{table}
\end{document}

Other approaches are also possible.

6
  • @Aku I am going out at the moment, will post an extension to the code in the morning for this.
    – yannisl
    Mar 2, 2012 at 13:07
  • Yes the book I am working on will always have two tables side by side all the time, no exception. your solution presents clean solution for table but how would I put them side by side as I have shown in MWE and also be able to show caption on top of the table
    – Aku
    Mar 2, 2012 at 13:13
  • Thanks for your help, I really appreciate it. However a small request, can you post your two table answer separate from what you have already given. Because your one table solution is worth keeping in the post as well. :)
    – Aku
    Mar 2, 2012 at 13:24
  • @Aku Please see edit
    – yannisl
    Mar 2, 2012 at 17:08
  • Any reason why you use two commands \starttable and \stoptable, instead of defining an environment (\begin{mytable}...\end{mytable}) for that? May 22, 2013 at 11:59
6

You can define your custom environment. The first two tables are from your MWE, the third is the result of:

\begin{MyTabular}{Table Name - 3}
\end{MyTabular}

Given the way environments work, the above can also be written as follows which is closer to what you desired:

\MyTabular{Table Name - 3}
\endMyTabular

enter image description here

\documentclass{article}

\newenvironment{MyTabular}[1]{% #1 = caption
    \begin{table}[!htbp]
    \centering
    \caption{#1}
    \renewcommand{\arraystretch}{1.1}
    \begin{tabular}{|c|c|c|c|}
        \hline
        Text-1  & Text-2 & Text-3 & Text-4 \\
        \hline
        &   &   &  \\
        &   &   &   \\
        &   &   &   \\
        \hline
}{%
    \end{tabular}
    \end{table}
}

\begin{document}

\begin{table}[!htbp]
\begin{minipage}[b]{0.5\linewidth}%\centering
{\caption{Table name - 1}
\renewcommand{\arraystretch}{1.1}
\begin{tabular}{|c|c|c|c|}

\hline
Text-1  & Text-2 & Text-3 & Text-4 \\
 \hline
   &   &   &  \\
   &   &   &  \\
   &   &   &  \\
\hline
\end{tabular}}

\end{minipage}
\hspace{0.5cm}
\begin{minipage}[b]{0.5\linewidth}
\centering
{\caption{Table Name - 2}
\renewcommand{\arraystretch}{1.1}
\begin{tabular}{|c|c|c|c|}
\hline
Text-1  & Text-2 & Text-3 & Text-4 \\
 \hline
   &   &   &  \\
   &   &   &  \\
   &   &   &  \\
\hline
\end{tabular}}

\end{minipage}
\end{table}

\begin{MyTabular}{Table Name - 3}
\end{MyTabular}
\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .