0

I want to have a table of width equal to the whole width of the page, I mean, obviously not counting the margins, just the space you are supposed to write in. Right now I'm using the array package and the commands

\newcolumntype{C}[1]{>{\centering\arraybackslash} m{#1\hsize}}
\newcolumntype{L}[1]{>{\arraybackslash} m{#1\hsize}}

To achieve this, if I set something like

{|C{0.14}|C{0.11}|L{0.75}|}

(0.14 + 0.11 + 0.75 = 1\hsize) However the table always overflows into the margin. Is there a way to fix this? Am I doing something wrong with those column types? Here is an example:

\documentclass[11pt,a4paper]{article}

%Packages
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage{parskip}
\usepackage[margin=2cm]{geometry}
\usepackage{array}
\usepackage{lipsum}


%opening
\title{Title}

\author{Pedro Ortiz}
\date{\today}

\begin{document}
    \begin{center}
        {\Huge\textbf{\textsc{Title}}}
    \end{center}
    \vspace*{1cm}
    \newcolumntype{C}[1]{>{\centering\arraybackslash} m{#1\hsize}}
    \newcolumntype{L}[1]{>{\arraybackslash} m{#1\hsize}}

    \begin{tabular}{|C{0.14}|C{0.11}|L{0.75}|}
        \hline
        \textbf{First title} & \textbf{Title} & \multicolumn{1}{>{\centering\arraybackslash} m{0.75\hsize}|}{\textbf{Very Large Title} }\\
        \hline 
        Some text that I decided to put here & \LaTeX & Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
        vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien
        est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinarat, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci egetrisus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum.\\ 
        \hline 
        More text that I decided to put here & \LaTeX & Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
        vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien
        est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinarat, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci egetrisus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum.\\
        \hline 
        Even more text that I decided to put here & \LaTeX & Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
        vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna. Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus eu tellus sit amet tortor gravida placerat. Integer sapien
        est, iaculis in, pretium quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean faucibus. Morbi dolor nulla, malesuada eu, pulvinarat, mollis ac, nulla. Curabitur auctor semper nulla. Donec varius orci egetrisus. Duis nibh mi, congue eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim rutrum.\\ 
        \hline 

    \end{tabular}
\end{document}

The tabularx package does not solve my problem, I also need the width to be customizable, if I want a table of three columns to be half of the width of the line, where the first column is of width .3(width of line) and the second and third are 0.1(width of line) it won't work. The X environment is also not appropriate for this as I need control over both the width and the text placement in each cell. I am sure there is a way to solve the width problem by redefining:

\newcolumntype{C}[1]{>{\centering\arraybackslash} m{#1\hsize}}
\newcolumntype{L}[1]{>{\arraybackslash} m{#1\hsize}}

But I don't know how to do it.

pjox
  • 389
  • Yes, I've seen those answers, however the tabularx package does not solve my problem, the X type is not enough for what I need because sometimes I also need control over the exact text alignment of each cell. If I set values like {|C{0.15}|C{0.125}|L{0.645}|} it works for example, for this table and for these margins. But I want something that is easily reproducible and customizable. I'm sure the problem is in the definition of the column types. – pjox Apr 15 '18 at 16:47
  • 1
    You can use X columns in the specifications of \newcolumntype as well and thereby changing the alignment –  Apr 15 '18 at 16:50
  • Yes, I've tried that, but again, it does not fix the proportion problem, if i want a table of three columns to be half of the width of the line, where the first column is of width .3(width of line) and the second and third are 0.1(width of line) it wont work, it will be more than half of the line – pjox Apr 15 '18 at 16:54
  • @pjox - Please study the user guide of the tabularx package. E.g., your table could be set up via the statement \begin{tabularx}{0.5\textwidth}{| >{\hsize=1.8\hsize}X | >{\hsize=0.6\hsize}X | >{\hsize=0.6\hsize}X |}. Observe that 1.8+0.6+0.6=3, which is the number of columns of type X in this tabularx environment, and that 1.8=3*0.6. – Mico Apr 15 '18 at 17:11
  • 1
    you want to use Steven Penny's answer, your table has three columns and 4 rules, so you need to start by subtracting 6\tabcolsep and 4\arrayrulewidth before distributing the remaining space to your m columns – David Carlisle Apr 15 '18 at 17:11
  • @pjox - What exactly is the statement {|C{0.15}|C{0.125}|L{0.645}| supposed to achieve? – Mico Apr 15 '18 at 17:14
  • @Mico That given certain margin lengths one can indeed find values that don't overflow the table, but the idea is that I don't want to calculate those values each time I change the margins. Also I don't want to use the X column type as sometimes I need the text to be centered. The problem is indeed the distribution as David Carlisle, pointed out. – pjox Apr 15 '18 at 17:24
  • 1
    you can use \centering on an X column so that isn't a reason not to use tabularx – David Carlisle Apr 15 '18 at 17:27
  • @DavidCarlisle, Yeah you are right, but It still leaves me with the problem of the widths of each column, which I also want to control by hand. So your other comment does work, I forgot, to account for the colsep and the arrayrulewidth. Something like: \newlength\myhsize \setlength\myhsize{\dimexpr \hsize - 6\tabcolsep - 4\arrayrulewidth} \newcolumntype{C}[1]{>{\centering\arraybackslash} m{#1\myhsize}} \newcolumntype{L}[1]{>{\arraybackslash} m{#1\myhsize}} Works, thank you for pointing me to the right answer! – pjox Apr 15 '18 at 17:35
  • @pjox - I don't understand the connection between (a) changing the margins and (b) changing the (relative) widths of the X-type columns. AFAICT, those two issues are entirely independent of each other. – Mico Apr 15 '18 at 17:36
  • @Mico It's because I was not accounting for the \tabcolsep and \arrayrulewidth, so changing the margins could make the table overflow or underflow. I've had that problem multiple times. – pjox Apr 15 '18 at 17:39
  • tabularx documents how to do exactly what you want allocate a fraction of the total to each X column, however the direct setting also has advantages (notably the table is only set once) – David Carlisle Apr 15 '18 at 17:39
  • @DavidCarlisle, Yes I was looking into that, thank you. – pjox Apr 15 '18 at 17:40
  • @Mico By the way, David Carlisle solved my problem, I don't think my question is exactly the same as the one you linked to. My problem and the solution I wanted was far more specific. I think David Carlisle's answer could also help someone else. If however you do not agree, let me know and I can erase it. – pjox Apr 15 '18 at 17:44

0 Answers0