0

I want to generate a table with fixed column width. This works for all the columns except for the last column.

Working example (fixed column width for all column except the last one)

\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{filecontents}

\begin{filecontents*}{grade.csv}
    name,givennamex,matriculation,gender,grade
    Maier,Hans,12348,m,1.0
    Huber,Anna,23456,f,2.3
    Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\begin{document}
    \begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}|c}%
        \bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
        \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
        {\\\hline\givennamex & \name & \matriculation}% specify your columns here
    \end{tabular}
\end{document}

Example with problem (fixed column width for all column including the last one)


\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{filecontents}

\begin{filecontents*}{grade.csv}
    name,givennamex,matriculation,gender,grade
    Maier,Hans,12348,m,1.0
    Huber,Anna,23456,f,2.3
    Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\begin{document}
    \begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}|>{\raggedright}m{2cm}}%
        \bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
        \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
        {\\\hline\givennamex & \name & \matriculation}% specify your columns here
    \end{tabular}
\end{document}

What can I do to set the width of the last column?

EDIT

After the comments below I added the package ragged2e and changed the first line

\begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}|>{\raggedright}m{2cm}}%

to this one

\begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}|m{2cm}}%

Overall, the code is now the following:

\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{filecontents}
\usepackage{ragged2e}

\begin{filecontents*}{grade.csv}
    name,givennamex,matriculation,gender,grade
    Maier,Hans,12348,m,1.0
    Huber,Anna,23456,f,2.3
    Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\begin{document}
    \begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}|m{2cm}}%
        \bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
        \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
        {\\\hline\givennamex & \name & \matriculation}% specify your columns here
    \end{tabular}
\end{document}

There is still one Problem: I cannot set the last column as centered (I tried to write >{\raggedright}m{2cm} instead of m{2cm} but it doesn't work).

marked as duplicate by leandriis, Stefan Pinnow, Raaja, Mensch, Andrew Swann Jul 5 at 12:25

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

To summarize the comment by Ulrike Fischer: here are three possibilites:

  1. Use >{\raggedright\arraybackslash} instad of >{\raggedright}.
  2. Define your own column type with \newcolumntype to avoid repeaded typing of the cde meintioned in 1.
  3. Use \tabularnewline instead of \\ to end a line in the table.

enter image description here

\documentclass{article}
\usepackage{array}
\usepackage{csvsimple}
\usepackage{filecontents}

\newcolumntype{R}[1]{>{\raggedright\arraybackslash}m{#1}}

\begin{filecontents*}{grade.csv}
    name,givennamex,matriculation,gender,grade
    Maier,Hans,12348,m,1.0
    Huber,Anna,23456,f,2.3
    Weisbaeck,Werner,34567,m,5.0
\end{filecontents*}

\begin{document}
\begin{tabular}{>{\raggedright\arraybackslash}m{2cm}|>{\raggedright\arraybackslash}m{2cm}|>{\raggedright\arraybackslash}m{2cm}}%
        \bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
        \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
        {\\\hline\givennamex & \name & \matriculation}% specify your columns here
\end{tabular}

\bigskip

\begin{tabular}{R{2cm}|R{2cm}|R{2cm}}%
        \bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
        \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
        {\\\hline\givennamex & \name & \matriculation}% specify your columns here
\end{tabular}

\bigskip

\begin{tabular}{>{\raggedright}m{2cm}|>{\raggedright}m{2cm}| >{\raggedright}m{2cm}}%
        \bfseries long title column 1 & \bfseries long title column 2 & \bfseries long title column 3% specify table head
        \csvreader[head to column names]{grade.csv}{}% use head of csv as column names
        {\tabularnewline\hline\givennamex & \name & \matriculation}% specify your columns here
\end{tabular}

\end{document}
  • many thanks for your answer @leandriis. However, how can I do so the last column ist then centered and not align on the right / left (cf. last comment in the question) – ecjb Jul 5 at 13:07
  • To horizontally center the last column replace \begin{tabular}{>{\raggedright\arraybackslash}m{2cm}|>{\raggedright\arraybackslash}m{2cm}|>{\raggedright\arraybackslash}m{2cm}} in the first table by \begin{tabular}{>{\raggedright\arraybackslash}m{2cm}|>{\raggedright\arraybackslash}m{2cm}|>{\centering\arraybackslash}m{2cm}} – leandriis Jul 6 at 15:41
  • Many Thanks @leandriis. That solved the problem – ecjb Jul 8 at 6:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.