1

I need to set up a table with three columns.

The first two columns have a width of 7.2 cm and the last has a width of 1.5 cm.

I would like text in the first row of the columns to be centred, but then the text in the first and third columns will remain centred, but the second column is aligned to the left.

I tried the tabularx suggestion on here, but can't set the column widths, let alone see how I can change the second column to left aligned.

  • 1
    Could you please add a screenshor of the desired output as well as a minimal working example (MWE) that show what you tried so far? – leandriis Oct 09 '19 at 06:44
  • 1
    Please also clarify: Is ist just the contents of the first row in every column that you want to horizontally center or do you want to horizontally center all rows of a specific column? – leandriis Oct 09 '19 at 06:45
  • If its the first case, you could use \multicolumn{1}{c} of \makecell (needs the makecell) package. If it's the second case, you could try with >{\centering\arraybackslash}p{7.2cm} instead. – leandriis Oct 09 '19 at 06:46
  • 1
    Also, are you sure that our textwidth is larger than 15.9 cm? Otherwise your table will overflow into the right margin. – leandriis Oct 09 '19 at 06:47
  • 1
    15.9cm + 6\tabcolsep! – Bernard Oct 09 '19 at 10:34
  • @leandriis here is what I've tried to start with:

    \begin{table}[h] \begin{tabularx}{\linewidth}{>{\hsize=7.2cm}c|>{\hsize=7.2cm}c|>{\hsize=1.5cm}X} \hline Column 1 & Column 2 & Col 3\\ \hline The text in this column needs to be centred and allow for text wrapping and will include equations & Text in this column needs to be aligned to the left and needs to allow for text wrapping & no wrap\\ \hline \end{tabularx} \end{table}

    – C. de Haer Oct 09 '19 at 13:22
  • @C.deHaer: >{\hsize=7.2cm}c will not work. Try with something like \begin{tabular}{>{\centering\arraybackslash}p{7.2cm}|p{7.2cm}|l} instead. – leandriis Oct 09 '19 at 13:38
  • @C.deHaer: With the above mentioned command, the table will be about 17 cm wide. Is you text block wide enough for that? – leandriis Oct 09 '19 at 13:40
  • @leandriis yes it should be fine. It's an A4 page with narrow margins. – C. de Haer Oct 09 '19 at 13:43
  • @leandriis I tried your suggestion and it works for Column 1, but the first row of Column 2 also needs to be centred. – C. de Haer Oct 09 '19 at 13:43
  • @C.deHaer: As already mentioned an a previous commant, you can use \multicolumn{1}{c}{Column 2} to center one cell in an otherwise differentl aligned column. However, please be aware that this might not work correctly (needs to be adjusted) if the contents of the cell would require a linebreak to fit into the respective column. – leandriis Oct 09 '19 at 13:48
  • Excellent, but now the | between column 2 and 3 has disappeared. – C. de Haer Oct 09 '19 at 13:56
  • 1
    \multicolumn{1}{c|}{Column 2}should work. – leandriis Oct 09 '19 at 14:09

1 Answers1

1

Various alignment in tables is very easy with tblr environment of tabularray package:

\documentclass{article}

\usepackage[a4paper,margin=1cm]{geometry} \usepackage{tabularray}

\begin{document}

\begin{table}[h] \centering \begin{tblr}{ colspec = {Q[7.2cm,c]|Q[7.2cm,l]|Q[1.5cm,c]}, row{1} = {c}, hlines, } Column 1 & Column 2 & Col 3 \ The text in this column needs to be centred and allow for text wrapping and will include equations & Text in this column needs to be aligned to the left and needs to allow for text wrapping & no wrap \ \end{tblr} \end{table}

\end{document}

enter image description here

L.J.R.
  • 10,932