9

Needless to say, a basic table can be generated as:

\documentclass[a4paper]{article}
\begin{document}
\begin{tabular}{|l|l|l|}\hline
name1       & name2     & name3        \\
stree1      & stree2    & stree3       \\
address1    & address2  & address3     \\
post code 1 & postcode2 & postcode 3  \\
\hline
\end{tabular}
\end{document}

But, for my purpose, I need to change the table frequently (say, I need to remove name1 and include name4 for one case and so on.)

Is it possible to create the table columnwise, say:

\begin{magic}
    \cola{name1; street1, address1, post code 1}
    \colb{name2; street2, address2, post code 2}
    \colc{name3; street3, address3, post code 3}
\end{magic}

to achieve the same output?

This is possibly same question as Create a table column by column with special border formatting But I can't find out whats going on there.

BaRud
  • 2,179

4 Answers4

5
\documentclass{article}

\newenvironment{magic}[1]{\valign\bgroup&\vfil\vbox{\parindent=0pt\tabskip=.67em\hsize=#1##}\vfil\cr}{\egroup}
\newcommand*\col[1]{#1\cr}

\begin{document}
\begin{magic}{25mm}
  \col{name1 & street1 & address1 & post code 1}
  \col{name2 & street2 & address2 & post code 2}
  \col{name3 & street3 & address3 & post code 3}
%  \col{name4 & street4 & address4 & post code 4}
\end{magic}
\end{document}

You might also instead say (which I slightly prefer):

...
\newcommand*\col[2]{\textbf{#1} && #2\cr}

\begin{document}
\begin{magic}{25mm}
  \col{name1}{street1 & address1 & post code 1}
  \col{name2}{street2 & address2 & post code 2}
  \col{name3}{street3 & address3 & post code 3}
%  \col{name4}{street4 & address4 & post code 4}
\end{magic}
\end{document}

output

Ruben
  • 13,448
2

You can do that with csvsimple:

\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier, erewhon}%

\usepackage{filecontents} %
\begin{filecontents}{test.csv}
name1, name2, name3
stree1, stree2, stree3
address1, address2 , address3
post code 1, postcode2, postcode 3
\end{filecontents}

\usepackage{array, csvsimple, booktabs}
\newcolumntype{$}{>{\global\let\currentrowstyle\relax}}
\newcolumntype{^}{>{\currentrowstyle}}
\newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}%
#1\ignorespaces
}

\csvstyle{mytablestyle}{before table=\renewcommand{\arraystretch}{1.3}\centering,tabular=$l^l^l, nohead, before first line=\\\toprule\rowstyle{\bfseries}, late after first line={\\ \addlinespace[0.4ex]}, late after last line=\\ \bottomrule}

\begin{document}

\csvreader[mytablestyle]{test.csv}{}{\csvcoli &\csvcolii &\csvcoliii}

\end{document}

enter image description here

Bernard
  • 271,350
1

enter image description here

one solutions can be by use of series tabulars

\documentclass[a4paper]{article}
\newcommand\col[1]{\begin{tabular}[t]{l}#1\end{tabular}}

\begin{document}
\col{name1\\ street1\\ address1\\ post code 1}%
\col{name1\\ street2\\ address2\\ post code 2}%
\col{name1\\ street3\\ address2\\ post code 3}

\end{document}
Mico
  • 506,678
Zarko
  • 296,517
1
\usepackage{varwidth}
...
\newcommand\col[1]{\begin{varwidth}{\textwidth}\let\\\newline#1\end{varwidth}}
\begin{tabular}{|*3{l|}}
  \hline
  \col{name1    \\ stree1   \\ address1 \\ post code1 }
  &
  \col{name2    \\ stree2   \\ address2 \\ post code2 }
  &
  \col{name3    \\ stree3   \\ address3 \\ post code3 } \\
\hline
\end{tabular}