页面无法容纳来自 csv 的表格

页面无法容纳来自 csv 的表格

我对 Latex 非常不熟悉,但我需要编写一个使用 csv 文件中的表格的文档,而我真的不知道该怎么做。主要问题:

  • 如果我从 csv 中包含标题,则表格太宽,并且它具有现在允许的字符,例如“[”、“]”和“`”;(我可以跳过标题并自己写吗?(标题总是一样的,但表格的内容因项目而异))
  • 表格太长,无法放入一页;(无法拆分。有没有办法说我只想要第一页的这些行数,然后再到下一页?)
  • 值前面有空格的将被替换为“”(请看图);
  • 我一直在尝试了解 longtable、tabularx、tabular 和 tabular* 的工作原理,但无济于事,尤其是当我将它与 csv 部分配对时(下面这是唯一对我有用的组合)
\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

当我维护 csv 文件中的标题时

如果我删除 csv 文件中的标题

答案1

如果您想要一个跨越给定内容的可破坏表格,xltabular 是一种方法。

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

在此处输入图片描述

答案2

@Bur 答案的一个小变化。主要区别是:

  • csv 文件重新组织:

    • 使用逗号代替分号
    • 使用小数点代替小数逗号。
  • 通过这种改变,他的解决方案开始为我工作,在此之前我得到了错误{\csvcoli & \csvcolii & \csvcoliii & \csvcoliv & \csvcolv & \csvcolvi & \csvcolvii} are undefined

  • 对于列的规范来自X派生的C{...}列类型,它将单元格内容居中,并能够简单地调整它们的宽度之间的比率

  • 对于第六列,使用 包S中定义的列说明符siunitx

梅威瑟:

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

其生产成果为:

在此处输入图片描述 在此处输入图片描述

(红线显示部分页面布局)

相关内容