好的,这就是我想要做的:
但我不明白如何从外部 CSV 文件创建此文件。我采用这种方法是因为这本书将由外部文件驱动,而这些文件由不了解 LaTeX 的人维护。
梅威瑟:
\documentclass[]{book}
\usepackage{currfile}
\usepackage{pgfplotstable}
\pgfplotstableset{col sep=semicolon, string type}
\usepackage{filecontents}
\begin{document}
\begin{filecontents*}{\currfilebase_information.csv}
Status;Active
Company;NXP
Formats;DIP16,SO16 etc.
Communication;Serial
Other names;bla
Type of HW;bla
\end{filecontents*}
\begin{table}[ht]
\centering
\pgfplotstabletypeset{\currfilebase_information.csv}
\caption{\currfilebase\ information}
\end{table}
\end{document}
电流输出:
答案1
需要做更多的工作来实现从文件读取。优点是灵活性更高。
在此代码中,可选参数是最大宽度。如果其中一个主条目比此宽度宽,则会缩小以适合。
\begin{filecontents*}{\jobname_information.csv}
Status;Active
Company;NXP
Formats;DIP16,SO16 etc.
Communication;Serial
Other names;bla
Type of HW;bla
\end{filecontents*}
\documentclass{article}
\usepackage{graphicx}
\ExplSyntaxOn
\NewDocumentCommand{\makestatustable}{O{\maxdimen}m}
{% #1 = optional width, default \maxdimen,
% #2 = file name
\jimmy_statustable:nn { #1 } { #2 }
}
\ior_new:N \g_jimmy_statustable_ior
\tl_new:N \l__jimmy_statustable_body_tl
\seq_new:N \l__jimmy_statustable_temp_seq
\box_new:N \l__jimmy_statustable_data_box
\cs_new_protected:Nn \jimmy_statustable:nn
{
\tl_clear:N \l__jimmy_statustable_body_tl
\ior_open:Nn \g_jimmy_statustable_ior { #2 }
\ior_map_inline:Nn \g_jimmy_statustable_ior
{
\__jimmy_statustable_line:nn { #1 } { ##1 }
}
\begin{tabular}{|l|}
\hline
\tl_use:N \l__jimmy_statustable_body_tl
\end{tabular}
}
\cs_new_protected:Nn \__jimmy_statustable_line:nn
{
\seq_set_split:Nnn \l__jimmy_statustable_temp_seq { ; } { #2 }
\__jimmy_statustable_head:x { \seq_item:Nn \l__jimmy_statustable_temp_seq { 1 } }
\__jimmy_statustable_data:nx { #1 } { \seq_item:Nn \l__jimmy_statustable_temp_seq { 2 } }
}
\cs_new_protected:Nn \__jimmy_statustable_head:n
{
\tl_put_right:Nn \l__jimmy_statustable_body_tl { \raisebox{0.5ex}{\tiny #1} \\ }
}
\cs_generate_variant:Nn \__jimmy_statustable_head:n { x }
\cs_new_protected:Nn \__jimmy_statustable_data:nn
{
\tl_put_right:Nn \l__jimmy_statustable_body_tl
{
\strut
\hbox_set:Nn \l__jimmy_statustable_data_box {#2}
\dim_compare:nTF { \box_wd:N \l__jimmy_statustable_data_box < #1 }
{
#2
}
{
\resizebox{#1}{!}{#2}
}
\\ \hline
}
}
\cs_generate_variant:Nn \__jimmy_statustable_data:nn { nx }
\ExplSyntaxOff
\begin{document}
\begin{table}[htp]
\centering\sffamily
\makestatustable[2cm]{\jobname_information.csv}
\caption{\jobname\ information}
\end{table}
\begin{table}[htp]
\centering\sffamily
\makestatustable{\jobname_information.csv}
\caption{\jobname\ information}
\end{table}
\end{document}
规则和数据之间的空间较少的版本;当表格处于其自然宽度且不缩放条目时,宽度与所述完全相同,除非没有可选的宽度参数。只要规定的宽度超过所有条目的宽度(不进行缩放),它就会处于自然宽度。
我没有对这些小标签进行缩放,假设它们适合规定的宽度。
\begin{filecontents*}{\jobname_information.csv}
Status;Active
Company;NXP
Formats;DIP16,SO16 etc.
Communication;Serial
Other names;bla
Type of HW;bla
\end{filecontents*}
\documentclass{article}
\usepackage{graphicx}
\usepackage{array}
\ExplSyntaxOn
\NewDocumentCommand{\makestatustable}{O{\maxdimen}m}
{% #1 = optional width, default \maxdimen,
% #2 = file name
\group_begin:
\setlength{\tabcolsep}{1pt} % reduce the space between rules and entries
\jimmy_statustable:xn { \dim_eval:n { #1 - 2\tabcolsep - 2\arrayrulewidth } } { #2 }
\group_end:
}
\ior_new:N \g_jimmy_statustable_ior
\tl_new:N \l__jimmy_statustable_body_tl
\seq_new:N \l__jimmy_statustable_temp_seq
\box_new:N \l__jimmy_statustable_data_box
\cs_new_protected:Nn \jimmy_statustable:nn
{
\tl_clear:N \l__jimmy_statustable_body_tl
\ior_open:Nn \g_jimmy_statustable_ior { #2 }
\ior_map_inline:Nn \g_jimmy_statustable_ior
{
\__jimmy_statustable_line:nn { #1 } { ##1 }
}
\begin{tabular}{|l|}
\hline
\tl_use:N \l__jimmy_statustable_body_tl
\end{tabular}
}
\cs_generate_variant:Nn \jimmy_statustable:nn { x }
\cs_new_protected:Nn \__jimmy_statustable_line:nn
{
\seq_set_split:Nnn \l__jimmy_statustable_temp_seq { ; } { #2 }
\__jimmy_statustable_head:x { \seq_item:Nn \l__jimmy_statustable_temp_seq { 1 } }
\__jimmy_statustable_data:nx { #1 } { \seq_item:Nn \l__jimmy_statustable_temp_seq { 2 } }
}
\cs_new_protected:Nn \__jimmy_statustable_head:n
{
\tl_put_right:Nn \l__jimmy_statustable_body_tl { \raisebox{0.5ex}{\tiny #1} \\ }
}
\cs_generate_variant:Nn \__jimmy_statustable_head:n { x }
\cs_new_protected:Nn \__jimmy_statustable_data:nn
{
\tl_put_right:Nn \l__jimmy_statustable_body_tl
{
\strut
\hbox_set:Nn \l__jimmy_statustable_data_box {#2}
\dim_compare:nTF { \box_wd:N \l__jimmy_statustable_data_box < #1 }
{
#2
}
{
\resizebox{#1}{!}{#2}
}
\\ \hline
}
}
\cs_generate_variant:Nn \__jimmy_statustable_data:nn { nx }
\ExplSyntaxOff
\begin{document}
\begin{table}[htp]
\centering\sffamily
\rule{2cm}{1pt}% to show that the width of the following table is exactly as stated
\medskip
\makestatustable[2cm]{\jobname_information.csv}
\caption{\jobname\ information}
\end{table}
\begin{table}[htp]
\centering\sffamily
\makestatustable{\jobname_information.csv}
\caption{\jobname\ information}
\end{table}
\end{document}
进一步版本
在这里你必须指定宽度。
\begin{filecontents*}{\jobname_information.csv}
Status;Active
Company;NXP
Formats;DIP16,SO16 etc.
Communication;Serial
Other names;bla
Type of HW;bla
\end{filecontents*}
\documentclass{article}
\usepackage{graphicx}
\usepackage{array}
\ExplSyntaxOn
\NewDocumentCommand{\makestatustable}{mm}
{% #1 = optional width, default \maxdimen,
% #2 = file name
\group_begin:
\setlength{\tabcolsep}{1pt}
\jimmy_statustable:xn { \dim_eval:n { #1 - 2pt - 2\arrayrulewidth } } { #2 }
\group_end:
}
\ior_new:N \g_jimmy_statustable_ior
\tl_new:N \l__jimmy_statustable_body_tl
\seq_new:N \l__jimmy_statustable_temp_seq
\box_new:N \l__jimmy_statustable_data_box
\cs_new_protected:Nn \jimmy_statustable:nn
{
\tl_clear:N \l__jimmy_statustable_body_tl
\ior_open:Nn \g_jimmy_statustable_ior { #2 }
\ior_map_inline:Nn \g_jimmy_statustable_ior
{
\__jimmy_statustable_line:nn { #1 } { ##1 }
}
\begin{tabular}{|w{l}{#1}|}
\hline
\tl_use:N \l__jimmy_statustable_body_tl
\end{tabular}
}
\cs_generate_variant:Nn \jimmy_statustable:nn { x }
\cs_new_protected:Nn \__jimmy_statustable_line:nn
{
\seq_set_split:Nnn \l__jimmy_statustable_temp_seq { ; } { #2 }
\__jimmy_statustable_head:x { \seq_item:Nn \l__jimmy_statustable_temp_seq { 1 } }
\__jimmy_statustable_data:nx { #1 } { \seq_item:Nn \l__jimmy_statustable_temp_seq { 2 } }
}
\cs_new_protected:Nn \__jimmy_statustable_head:n
{
\tl_put_right:Nn \l__jimmy_statustable_body_tl { \raisebox{0.5ex}{\tiny #1} \\ }
}
\cs_generate_variant:Nn \__jimmy_statustable_head:n { x }
\cs_new_protected:Nn \__jimmy_statustable_data:nn
{
\tl_put_right:Nn \l__jimmy_statustable_body_tl
{
\strut
\hbox_set:Nn \l__jimmy_statustable_data_box {#2}
\dim_compare:nTF { \box_wd:N \l__jimmy_statustable_data_box < #1 }
{
#2
}
{
\resizebox{#1}{!}{#2}
}
\\ \hline
}
}
\cs_generate_variant:Nn \__jimmy_statustable_data:nn { nx }
\ExplSyntaxOff
\begin{document}
\begin{table}[htp]
\centering\sffamily
\rule{2cm}{1pt}% to show that the width of the following table is exactly as stated
\medskip
\makestatustable{2cm}{\jobname_information.csv}
\caption{\jobname\ information}
\end{table}
\begin{table}[htp]
\centering\sffamily
\rule{5cm}{1pt}% to show that the width of the following table is exactly as stated
\medskip
\makestatustable{5cm}{\jobname_information.csv}
\caption{\jobname\ information}
\end{table}
\end{document}
答案2
我将使用该csvsimple
包来处理 csv。您可以对 csv 文件执行任意操作。大致如下:
\documentclass{book}
\usepackage{currfile}
\usepackage{filecontents}
\usepackage{csvsimple}
\begin{document}
\begin{filecontents*}{\currfilebase_information.csv}
Status;Active
Company;NXP
Formats;DIP16,SO16 etc.
Communication;Serial
Other names;bla
Type of HW;bla
\end{filecontents*}
{\sffamily
\csvreader[
tabular = |@{\,}l|,
table head = \hline,
/csv/separator=semicolon,
late after line = \\\hline
]{\currfilebase_information.csv}{}%
{{\raisebox{0.3ex}{\tiny\csvcoli}} \\ {\large\,\csvcolii} }
}
\end{document}
看其文档。