不用说,可以生成一个基本表格,如下所示:
\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}
但是,就我的目的而言,我需要经常更改表格(比如,我需要删除name1
并包含name4
一个案例等等。)
是否可以按列创建表,例如:
\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}
达到同样的输出效果?
这可能是同一个问题逐列创建具有特殊边框格式的表格 但我不知道那里发生了什么。
答案1
\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}
你也可以这样说(我更喜欢这样):
...
\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}
答案2
你可以这样做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}
答案3
答案4
\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}