Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Latex Table Problem

  • 31-01-2015 9:12pm
    #1
    Registered Users, Registered Users 2 Posts: 5,633 ✭✭✭


    Hi folks. I could do with a bit of help. I have the following code for a table. What I want is for the text in the cells to be centred vertically as well as horizontally. Any help would be appreciated!



    \begin{document}
    \begin{center}
    {\footnotesize
    \begin{tabular}{ | p{2cm} | c | c | c | c | c | c |}
    \hline
    Time in days from previous earthquake & $0\leq x< 100$ & $100\leq x< 200$ & $200\leq x< 300$ & $300\leq x< 400$ & $400\leq x< 500$ \\ \hline
    Number of earthquakes & 31 & 24 & 12 & 14 & 8 \\ \hline
    \end{tabular}
    }
    \end{center}
    \end{document}


Comments

  • Registered Users, Registered Users 2 Posts: 26,578 ✭✭✭✭Turtwig


    The default c or 'center' command should do that already for you. Is there any particular reason to use the p(2cm)? I'd just use |c| all the way let LaTeX decide the spacing distances it requires.

    hth


  • Registered Users, Registered Users 2 Posts: 5,633 ✭✭✭TheBody


    I need the P{2cm} to make latex wrap the text in the first column. It will push the table off the page if I don't.

    The c centres the text horizontally for me but I don't know how to centre it vertically too. That's my problem.


  • Registered Users, Registered Users 2 Posts: 26,578 ✭✭✭✭Turtwig


    Ack! My bad. :o Should have seen that. Apologies.

    Well such is the excitement of my saturday night I spent the last hour tweaking and messing with the tabular environment and I (sorta) stumbled upon a solution.

    \begin{tabular}{ | p{2.5cm} | c | c | c | c | c | c |}
    \hline
    Time in days &&&&&\\
    from previous
    & $0\leq x< 100$ & $100\leq x< 200$ & $200\leq x< 300$ & $300\leq x< 400$ & $400\leq x< 500$ \\
    earthquake &&&&&\\
    \hline

    :o

    In so far as I can tell without changing package there's no other solution. Changing package and the best option seems tabu, but reading up on the documentation implies it's no longer supported so you may end up writing a document only to find a few months down the road it doesn't render correctly.
    Failing that there was tabulary which kind of did what you want but the inequalities looked really ugly. Fugly actually.

    Most aesthetic solution I got is above but I may return to this in a wee while. :o


  • Registered Users, Registered Users 2 Posts: 5,633 ✭✭✭TheBody


    Thanks for your efforts. In the end I scoured the internet for some existing code to rob!

    This seems to work perfectly although there is a lot of stuff in the code that I am not familiar with.

    \begin{table}[h]
    \begin{center}
    {\footnotesize
    \begin{tabular}{| >{\centering\arraybackslash}m{0.8in} | >{\centering\arraybackslash}m{0.8in} | >{\centering\arraybackslash}m{0.9in} | >{\centering\arraybackslash}m{0.9in} |>{\centering\arraybackslash}m{0.9in} |>{\centering\arraybackslash}m{0.9in} |}\hline
    Time in days from previous earthquake & $0\leq x< 100$ & $100\leq x< 200$ & $200\leq x< 300$ & $300\leq x< 400$ & $400\leq x< 500$\\[0ex] \hline
    Number of earthquakes & 31 & 24 & 12 & 14 & 8 \\[0ex] \hline
    \end{tabular}
    }
    \end{center}
    \end{table}


Advertisement