0

Ok, this is what I want to make:

enter image description here

But I'm struggling with understanding how to make this from an external CSV file. I have this approach because the book will be driven by external files which is maintained by people with no LaTeX knowledge.

MWE:

\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}

Current output:

enter image description here

2 Answers 2

1

With some more work for implemeting the reading from the file. The advantage is greater flexibility.

In this code the optional argument is the maximum width. If one of the main entries is wider than this, it is scaled down to fit.

\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}

enter image description here

A version with less space betwee the rules and the data; the width is exactly as stated, unless there's no optional width argument, when the table is at its natural width with no scaling of the entries. It will be at natural width any time the stated width exceeds that of all the entries (no scaling up is done).

I did no scaling to the tiny labels, assuming they'll fit in the stated width.

\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}

enter image description here

Further version

Here you must specify the width.

\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}

enter image description here

7
  • Wow, looks really promising! I think it will be the way to go. May 12, 2022 at 21:29
  • I cannot get this to compile. "Undefined control sequence. l.14 \NewDocumentCommand {\makestatustable}{O{\maxdimen}m}" May 16, 2022 at 15:27
  • 1
    @JimmyWestberg Update your TeX system. In the meantime, add \usepackage{xparse}.
    – egreg
    May 16, 2022 at 15:30
  • 1
    @JimmyWestberg Add \usepackage{array}.
    – egreg
    May 16, 2022 at 16:19
  • 1
    @JimmyWestberg That's what I wrote in the answer, isn't it? Further version added. Please remove the outdated comments.
    – egreg
    May 18, 2022 at 9:51
3

I would use the csvsimple package for processing the csv. You can do arbitrary things with csv files. Something along these lines:

\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}

csvsimple output

See its documentation.

1
  • Great! I will test next week when I'm back at work. May 12, 2022 at 20:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .