0

I'm very unexperienced in Latex but I need to write a document that uses table from a csv file and I'm having a really hard time knowing how to do it. The main problems:

  • If I include the header from csv the table is too wide and it has caracters that are now alowed such as "[", "]" and "`"; (Can I skip the header and write it myself? (The header is always the same but the content of the table changes from project to project))
  • The table is too long to fit into a page; (Can't break it. Is there a way to say I just want these amount of rows for 1 page and then of to the next one?)
  • The values have spaces before are substituted with "" (see image please);
  • I have been trying to understand how longtable, tabularx, tabular and tabular* work, but to no avail, especially when I pair it with the csv part (below it's the only combination that has sort of worked for me)
\documentclass[a4paper,12pt]{article}
\usepackage{graphicx} % adds images
\usepackage{subcaption}
\usepackage{setspace} % set the spacing for individual parts of the document
\usepackage{booktabs}
\usepackage[a4paper, portrait, margin=2.5cm]{geometry} % defines margins
\usepackage{fancyhdr} % header and footer
\usepackage{lipsum} % imagens in header
\usepackage{pdfpages} % automatically numbers pdf pages
\usepackage{xcolor} % adds color to text
\usepackage[utf8]{inputenc}
\usepackage[default]{sourcesanspro} % adds a similar font to Myriad Pro
\usepackage[T1]{fontenc}% adds a similar font to Myriad Pro
\usepackage{stringstrings} % \substring{abcdefgh}{3}{6} gives me the 3rd to 6th char = "cdef"
\usepackage{currfile} % gets filename
\usepackage{csvsimple}
\usepackage{longtable} %separates tables
\usepackage{tabularx} %separates tables

%code that was online to change the date format \def\mydate{\leavevmode\hbox{\twodigits\day-\twodigits\month-\the\year}} \def\twodigits#1{\ifnum#1<10 0\fi\the#1}

\pagestyle{fancy} \fancyhf{} %elimina header e footer default \renewcommand{\headrulewidth}{0pt} %elimina linha entre cabeçalho e texto \renewcommand{\footrulewidth}{0pt} \setlength\headheight{45.0pt} \addtolength{\textheight}{-45.0pt} \lhead{\includegraphics[width=6cm]{auxiliar/header.jpg}} \rfoot{\textcolor{gray}{\thepage}} \lfoot{\includegraphics[width=15cm]{auxiliar/footer.jpg}}

\begin{document}

\begin{tabularx}{\textwidth}{l *{6}{>{\centering\arraybackslash}X} } \csvautotabular[separator=semicolon, respect all]{23.csv} \end{tabularx}

\end{document}

%csv with headers content
Type;Nombre;Longueur [m];Poids unitaire [kG/m];Poids piece [kG];Poids total [kG];Surf. peinture [m2]
S 275;;;;;;
    CAE 50x5;2;6,02;3,77;22,70;45;2,34
    CAE 50x5;4;6,27;3,77;23,65;95;4,87
    CAE 50x5;4;6,28;3,77;23,68;95;4,87
    CAE 50x5;4;6,29;3,77;23,72;95;4,88
    CAE 50x5;4;6,30;3,77;23,76;95;4,89
    CAE 50x5;4;6,38;3,77;24,06;96;4,95
    CAE 50x5;4;6,39;3,77;24,10;96;4,96
    CAE 50x5;4;6,46;3,77;24,36;97;5,01
    CAE 50x5;4;6,66;3,77;25,12;100;5,17
    CAE 50x5;4;6,67;3,77;25,16;101;5,18
%csv without headers content
S 275;;;;;;
    CAE 50x5;2;6,02;3,77;22,70;45;2,34
    CAE 50x5;4;6,27;3,77;23,65;95;4,87
    CAE 50x5;4;6,28;3,77;23,68;95;4,87
    CAE 50x5;4;6,29;3,77;23,72;95;4,88
    CAE 50x5;4;6,30;3,77;23,76;95;4,89
    CAE 50x5;4;6,38;3,77;24,06;96;4,95
    CAE 50x5;4;6,39;3,77;24,10;96;4,96
    CAE 50x5;4;6,46;3,77;24,36;97;5,01
    CAE 50x5;4;6,66;3,77;25,12;100;5,17
    CAE 50x5;4;6,67;3,77;25,16;101;5,18

When I mantain the headers in my csv file

If I remove the headers in my csv file

  • Please, provide a full minimal working example. You can put some lines from your csv file using the filecontents environment. – NBur Feb 10 '21 at 12:22
  • 1
    You should make column headers narrower. For example with use abbreviation and/or have text in two lines. – Zarko Feb 10 '21 at 12:29
  • @NBur Thanks, I added a few lines of the csv to the original post. Hope it's ok – Ana Cláudia Faria Feb 10 '21 at 12:30
  • @Zarko How can I make the text go to two lines? – Ana Cláudia Faria Feb 10 '21 at 12:31
  • One way is to use tabularx table: (i) in document preamble add \usepackage{tabularx}, (ii) table define as \begin{tabularx}{\textwidth}{l *{6}{>{\centering\arraybackslash}X} } \csvautotabular[separator=semicolon, respect all]{23.csv} \end{tabularx} – Zarko Feb 10 '21 at 12:36
  • @Zarko when I try I get this error. Am I doing something wrong? https://pasteboard.co/JNHSW0f.jpg

    (I'm sorry if it's a simple mistake, I haven't work with Latex before)

    – Ana Cláudia Faria Feb 10 '21 at 12:40
  • 1
    tabularx requires 2 mandatory arguments: the tabular width and the columns specification. Just as in @Zarko's comment ;) – NBur Feb 10 '21 at 12:42
  • Sorry, I cant provide an MWE (Minimal working Example) which you can test because you not show us an example of document which reproduce your problem. – Zarko Feb 10 '21 at 12:44
  • I tried that solution and it doesn't rearrange the width of the columns. Please see the image: link and here are the files: link @Zarko – Ana Cláudia Faria Feb 10 '21 at 12:59
  • Please, copy your document code to question. Link is not visible to all and cab become dead after while. – Zarko Feb 10 '21 at 13:02
  • @Zarko, thanks, I've copied it – Ana Cláudia Faria Feb 10 '21 at 13:05

2 Answers2

3

If you want both a breakable table that span a given, xltabular is a way.

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{csvsimple}
\usepackage{booktabs} % nice format of tables
\usepackage{xltabular} % mix between longtable and tabularx
\usepackage{siunitx} % to format units

\begin{filecontents}{23.csv} Type;Nombre;Longueur [m];Poids unitaire [kG/m];Poids piece [kG];Poids total [kG];Surf. peinture [m2] S 275;;;;;; CAE 50x5;2;6,02;3,77;22,70;45;2,34 CAE 50x5;4;6,27;3,77;23,65;95;4,87 CAE 50x5;4;6,28;3,77;23,68;95;4,87 CAE 50x5;4;6,29;3,77;23,72;95;4,88 CAE 50x5;4;6,30;3,77;23,76;95;4,89 CAE 50x5;4;6,38;3,77;24,06;96;4,95 CAE 50x5;4;6,39;3,77;24,10;96;4,96 CAE 50x5;4;6,46;3,77;24,36;97;5,01 CAE 50x5;4;6,66;3,77;25,12;100;5,17 CAE 50x5;4;6,67;3,77;25,16;101;5,18 S 275;;;;;; CAE 50x5;2;6,02;3,77;22,70;45;2,34 CAE 50x5;4;6,27;3,77;23,65;95;4,87 CAE 50x5;4;6,28;3,77;23,68;95;4,87 CAE 50x5;4;6,29;3,77;23,72;95;4,88 CAE 50x5;4;6,30;3,77;23,76;95;4,89 CAE 50x5;4;6,38;3,77;24,06;96;4,95 CAE 50x5;4;6,39;3,77;24,10;96;4,96 CAE 50x5;4;6,46;3,77;24,36;97;5,01 CAE 50x5;4;6,66;3,77;25,12;100;5,17 CAE 50x5;4;6,67;3,77;25,16;101;5,18 S 275;;;;;; CAE 50x5;2;6,02;3,77;22,70;45;2,34 CAE 50x5;4;6,27;3,77;23,65;95;4,87 CAE 50x5;4;6,28;3,77;23,68;95;4,87 CAE 50x5;4;6,29;3,77;23,72;95;4,88 CAE 50x5;4;6,30;3,77;23,76;95;4,89 CAE 50x5;4;6,38;3,77;24,06;96;4,95 CAE 50x5;4;6,39;3,77;24,10;96;4,96 CAE 50x5;4;6,46;3,77;24,36;97;5,01 CAE 50x5;4;6,66;3,77;25,12;100;5,17 CAE 50x5;4;6,67;3,77;25,16;101;5,18 S 275;;;;;; CAE 50x5;2;6,02;3,77;22,70;45;2,34 CAE 50x5;4;6,27;3,77;23,65;95;4,87 CAE 50x5;4;6,28;3,77;23,68;95;4,87 CAE 50x5;4;6,29;3,77;23,72;95;4,88 CAE 50x5;4;6,30;3,77;23,76;95;4,89 CAE 50x5;4;6,38;3,77;24,06;96;4,95 CAE 50x5;4;6,39;3,77;24,10;96;4,96 CAE 50x5;4;6,46;3,77;24,36;97;5,01 CAE 50x5;4;6,66;3,77;25,12;100;5,17 CAE 50x5;4;6,67;3,77;25,16;101;5,18 \end{filecontents}

\begin{document} \begin{xltabular}{\linewidth}{@{} l *{6}{>{\centering\arraybackslash}X} @{}} \caption{Manual formating}\ % first header \toprule Type & Nombre & L (\si{\meter}) & Poids unitaire (\si{\kilogram\per\metre}) & Poids piece (\si{\kilogram}) & Poids total (\si{\kilogram}) & Surf. peinture (\si{\meter\squared})\ \midrule \endfirsthead % next headers {\ldots\ \small suite}\ \toprule Type & Nombre & L (\si{\meter}) & Poids unitaire (\si{\kilogram\per\metre}) & Poids piece (\si{\kilogram}) & Poids total (\si{\kilogram}) & Surf. peinture (\si{\meter\squared})\ \midrule \endhead % footer at split \midrule \multicolumn{7}{r@{}}{\small à suivre\ldots} \endfoot % last footer \bottomrule \endlastfoot % content \csvreader[% late after line=\, late after last line={}, filter test=\ifnumgreater{\thecsvinputline}{1}, % remove first row separator=semicolon, respect all, ]{23.csv}{}{\csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi & \csvcolvii} \end{xltabular} \end{document}

enter image description here

NBur
  • 4,326
  • 10
  • 27
  • Thank you very much! I copied your example but still get an error "Missing $ inserted". Any ideias of what it could be? – Ana Cláudia Faria Feb 10 '21 at 14:37
  • 1
    +1, however, for all interested for cvs table which like to test your solution, it will be fine to add to your MWE the content of the table (that they not need to wrote separate files with it content). – Zarko Feb 10 '21 at 14:54
  • @AnaCláudiaFaria if I add for instantce an underscore somewhere, I've got the error too, but the pdf renders accordingly… Maybe there is something similar here? – NBur Feb 10 '21 at 15:03
  • @NBur yes I had an underscore in the original csv file! I deleted it and it works great! Thank you so much for your help! Much appreciated! – Ana Cláudia Faria Feb 10 '21 at 15:13
  • Thank you for adding filecontents. Unfortunately I cant reproduce your result. Table contain only the first block of data ... – Zarko Feb 10 '21 at 15:14
  • My fault, use filecontents* (or remove the first commented lines in the csv file). – NBur Feb 10 '21 at 15:22
1

A small variation of @Bur answer. Main diference are:

  • csv file is reorganized:

    • instead of semicolons are used comas
    • instead of decimal comas are used decimal points.
  • by this changes his solution start to work for me, before that I got errors {\csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi & \csvcolvii} are undefined

  • for columns specification is from X derived C{...} solumn type, which center the cells contents and enable simple adjust ratios between their widths

  • for 6th column is used S column specifier defined in the siunitx package

MWE:

\documentclass{article}

\usepackage{filecontents} \begin{filecontents}{test.csv} A, B, C, D, E, F, G, S 175 , , , , , , , CAE 50x5, 2, 6.02, 3.77, 22.70, 45, 2.34, CAE 50x5, 4, 6.27, 3.77, 23.65, 95, 4.87, CAE 50x5, 4, 6.28, 3.77, 23.68, 95, 4.87, CAE 50x5, 4, 6.29, 3.77, 23.72, 95, 4.88, CAE 50x5, 4, 6.30, 3.77, 23.76, 95, 4.89, CAE 50x5, 4, 6.38, 3.77, 24.06, 96, 4.95, CAE 50x5, 4, 6.39, 3.77, 24.10, 96, 4.96, CAE 50x5, 4, 6.46, 3.77, 24.36, 97, 5.01, CAE 50x5, 4, 6.66, 3.77, 25.12, 100, 5.17, CAE 50x5, 4, 6.67, 3.77, 25.16, 101, 5.18, S 275 , , , , , , , CAE 50x5, 2, 6.02, 3.77, 22.70, 45, 2.34, CAE 50x5, 4, 6.27, 3.77, 23.65, 95, 4.87, CAE 50x5, 4, 6.28, 3.77, 23.68, 95, 4.87, CAE 50x5, 4, 6.29, 3.77, 23.72, 95, 4.88, CAE 50x5, 4, 6.30, 3.77, 23.76, 95, 4.89, CAE 50x5, 4, 6.38, 3.77, 24.06, 96, 4.95, CAE 50x5, 4, 6.39, 3.77, 24.10, 96, 4.96, CAE 50x5, 4, 6.46, 3.77, 24.36, 97, 5.01, CAE 50x5, 4, 6.66, 3.77, 25.12, 100, 5.17, CAE 50x5, 4, 6.67, 3.77, 25.16, 101, 5.18, S 375 , , , , , , , CAE 50x5, 2, 6.02, 3.77, 22.70, 45, 2.34, CAE 50x5, 4, 6.27, 3.77, 23.65, 95, 4.87, CAE 50x5, 4, 6.28, 3.77, 23.68, 95, 4.87, CAE 50x5, 4, 6.29, 3.77, 23.72, 95, 4.88, CAE 50x5, 4, 6.30, 3.77, 23.76, 95, 4.89, CAE 50x5, 4, 6.38, 3.77, 24.06, 96, 4.95, CAE 50x5, 4, 6.39, 3.77, 24.10, 96, 4.96, CAE 50x5, 4, 6.46, 3.77, 24.36, 97, 5.01, CAE 50x5, 4, 6.66, 3.77, 25.12, 100, 5.17, CAE 50x5, 4, 6.67, 3.77, 25.16, 101, 5.18, S 475 , , , , , , , CAE 50x5, 2, 6.02, 3.77, 22.70, 45, 2.34, CAE 50x5, 4, 6.27, 3.77, 23.65, 95, 4.87, CAE 50x5, 4, 6.28, 3.77, 23.68, 95, 4.87, CAE 50x5, 4, 6.29, 3.77, 23.72, 95, 4.88, CAE 50x5, 4, 6.30, 3.77, 23.76, 95, 4.89, CAE 50x5, 4, 6.38, 3.77, 24.06, 96, 4.95, CAE 50x5, 4, 6.39, 3.77, 24.10, 96, 4.96, CAE 50x5, 4, 6.46, 3.77, 24.36, 97, 5.01, CAE 50x5, 4, 6.66, 3.77, 25.12, 100, 5.17, CAE 50x5, 4, 6.67, 3.77, 25.16, 101, 5.18, S 575 , , , , , , , CAE 50x5, 2, 6.02, 3.77, 22.70, 45, 2.34, CAE 50x5, 4, 6.27, 3.77, 23.65, 95, 4.87, CAE 50x5, 4, 6.28, 3.77, 23.68, 95, 4.87, CAE 50x5, 4, 6.29, 3.77, 23.72, 95, 4.88, CAE 50x5, 4, 6.30, 3.77, 23.76, 95, 4.89, CAE 50x5, 4, 6.38, 3.77, 24.06, 96, 4.95, CAE 50x5, 4, 6.39, 3.77, 24.10, 96, 4.96, CAE 50x5, 4, 6.46, 3.77, 24.36, 97, 5.01, CAE 50x5, 4, 6.66, 3.77, 25.12, 100, 5.17, CAE 50x5, 4, 6.67, 3.77, 25.16, 101, 5.18, S 675 , , , , , , , CAE 50x5, 2, 6.02, 3.77, 22.70, 45, 2.34, CAE 50x5, 4, 6.27, 3.77, 23.65, 95, 4.87, CAE 50x5, 4, 6.28, 3.77, 23.68, 95, 4.87, CAE 50x5, 4, 6.29, 3.77, 23.72, 95, 4.88, CAE 50x5, 4, 6.30, 3.77, 23.76, 95, 4.89, CAE 50x5, 4, 6.38, 3.77, 24.06, 96, 4.95, CAE 50x5, 4, 6.39, 3.77, 24.10, 96, 4.96, CAE 50x5, 4, 6.46, 3.77, 24.36, 97, 5.01, CAE 50x5, 4, 6.66, 3.77, 25.12, 100, 5.17, CAE 50x5, 4, 6.67, 3.77, 25.16, 101, 5.18, \end{filecontents} \usepackage{csvsimple}

\usepackage{geometry} \usepackage{siunitx} \usepackage{booktabs, xltabular} \newcolumntype{C}[1]{>{\centering\arraybackslash% \hsize=#1\hsize}X} \NewExpandableDocumentCommand\mcx{O{C{1}}m} {\multicolumn{1}{#1}{#2}} \usepackage[skip=1ex]{caption} %---------------- show page layout. don't use in a real document! \usepackage{showframe} \renewcommand\ShowFrameLinethickness{0.15pt} \renewcommand*\ShowFrameColor{\color{red}} %---------------------------------------------------------------%

\begin{document} \begin{xltabular}{\linewidth}{ l *{2}{C{0.8}} C{1.4} C{0.9} S[table-format=3.0] C{1.2} }% \caption{Manual formatting} \label{longtab:cvs} \ \toprule Type & Nombre & L\par (\si{\meter}) & Poids unitaire (\si{\kilogram\per\metre}) & Poids piece (\si{\kilogram}) & \mcx[C{0.9}]{Poids total (\si{\kilogram})} & Surf. peinture (\si{\meter\squared}) \ \midrule \endfirsthead \caption[]{Manual formatting (suite)} \ \toprule Type & Nombre & L (\si{\meter}) & Poids unitaire (\si{\kilogram\per\metre}) & Poids piece (\si{\kilogram}) & \mcx[C{0.9}]{Poids total (\si{\kilogram})} & Surf. peinture (\si{\meter\squared}) \ \midrule \endhead \midrule \multicolumn{7}{r@{}}{\small à suivre\ldots} \endfoot \bottomrule \endlastfoot \csvreader[late after line=\, ]{test.csv}{}% {\csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi & \csvcolvii}% \end{xltabular}

\end{document}

which produce:

enter image description here enter image description here

(red lines shows a part of the page layout)

Zarko
  • 296,517