How to create new table environment
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}
\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.
\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}
\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.
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
\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}