2

Below is my code

Normal Table:

\documentclass{article}


\begin{document}

\begin{table}
\caption{Sample Table.\label{tab:active_distance}}
\begin{center}
\begin{tabular}{lcccc}
\hline
H1 & H2 & H3& H4 & H5\\
\hline
Cell 1&Cell 2& Cell 3& Cell 4&Cell 5\\
Cell 1&Cell 2& Cell 3& Cell 4&Cell 5\\
\hline
\end{tabular}
\end{center}
\end{table}

\end{document}

and i need to use my custom tags like the one mentioned below for conversion and re use purposes, Customized Table :

\documentclass{article}
\begin{document}
\begin{table}
\caption{Sample Table.\label{tab:active_distance}}
\begin{center}
\begin{tabular}{lcccc}
\hline
\thead{
\tr{\td{H1}\td{H2}\td{H3}\td{H4}\td{H5}}}
\hline
\tbody{
\tr{\td{Cell 1}\td{Cell 2}\td{Cell 3}\td{Cell 4}\td{Cell 5}}}
\tr{\td{Cell 1}\td{Cell 2}\td{Cell 3}\td{Cell 4}\td{Cell 5}}}
 }
\hline
\end{tabular}
\end{center}
\end{table}
\end{document}

Is it possible to define the below to create a table, please guide

\thead{} - Table Head
 \tbody{} - Table Body
 \tr{} - Table row
 \td{} - Table Cell

Is this Possible, Thanks in Advance,

1

An implementation with expl3:

\documentclass{article}

\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentEnvironment{itabular}{O{c}m}% 'implicit' tabular
 {% 1. change the meaning of \thead, \tbody, \tr and \td
  % actually \thead and \tbody are the same, while \tr and \td are just markers
  \cs_set_eq:NN \thead \itabular_tbody:n
  \cs_set_eq:NN \tbody \itabular_tbody:n
  % 2. make the tabular
  \begin{tabular}[#1]{#2}
 }
 {
  \end{tabular}
 }

\tl_new:N \l_itabular_body_tl
\seq_new:N \l_itabular_rows_seq
\seq_new:N \l_itabular_one_row_seq

\cs_new_protected:Nn \itabular_tbody:n
 {
  \tl_clear:N \l_itabular_body_tl
  % divide at \tr, the first item is empty and discarded
  \seq_set_split:Nnn \l_itabular_rows_seq { \tr } { #1 }
  \seq_pop_left:NN \l_itabular_rows_seq \l_tmpa_tl
  \seq_map_inline:Nn \l_itabular_rows_seq
   {
    % divide at \td, the first item is empty and discarded
    \seq_set_split:Nnn \l_itabular_one_row_seq { \td } { ##1 }
    \seq_pop_left:NN \l_itabular_one_row_seq \l_tmpa_tl
    % fill in the body, items are separated by &, add \\ at the end
    \tl_put_right:Nx \l_itabular_body_tl
     {
      \seq_use:Nn \l_itabular_one_row_seq { & }
      \exp_not:N \\
     }
   }
  % deliver the body
  \tl_use:N \l_itabular_body_tl
 }
\ExplSyntaxOff

\begin{document}

\begin{tabular}{lcccc}
\hline
H1 & H2 & H3& H4 & H5\\
\hline
Cell 1&Cell 2& Cell 3& Cell 4&Cell 5\\
Cell 1&Cell 2& Cell 3& Cell 4&Cell 5\\
\hline
\end{tabular}

\bigskip

\begin{itabular}{lcccc}
\hline
\thead{
  \tr{\td{H1}\td{H2}\td{H3}\td{H4}\td{H5}}
}
\hline
\tbody{
  \tr{\td{Cell 1}\td{Cell 2}\td{Cell 3}\td{Cell 4}\td{Cell 5}}
  \tr{\td{Cell 1}\td{Cell 2}\td{Cell 3}\td{Cell 4}\td{Cell 5}}
}
\hline
\end{itabular}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.