2

So I have this table, which I just used an online tool to create, but I want the columns to wrap. So, I just change \begin{tabular} to \begin{tabularx} and then the last column vertical line is about 1cm from the table... How can I fix that gap?

\begin{tabularx}{\textwidth}{|X|X|X|X|}
\hline
Name:                                      & Donald Trump                              & Student ID:                                         & 99999999                                     \\ \hline
\multicolumn{2}{|p{0.5\textwidth}|}{Feedback focus:}                                                 & \multicolumn{2}{p{0.5\textwidth}|}{asoidjfaopsidjfp oiajdfpo iasjdfp oiasjdfpo ij}                                \\ \hline
\multicolumn{2}{|p{0.5\textwidth}|}{askdljf ;laksjdf;l aksjdf;lkajsd;:}                          & \multicolumn{2}{l|}{as;ldifja;lsdkfja;lsdkfja; slkfj; aldkjf ;aslkdj ;alk}                         \\ \hline \hline
\multicolumn{4}{|p{\textwidth}|}{as;ldkf;lkj: aklsduhf asldkj f;alskdjf ;alskdjf ;alskdf }                                                                  \\ \hline
Fully Met                                  & Met                                      & Partially Met                                       & Not Met                                      \\ \hline
Comments:                                  & \multicolumn{3}{l|}{}                                                                                                                         \\ \hline
\multicolumn{4}{|p{\textwidth}|}{asldkjf;alksdj f;laksjdf:as;dlk ja;lsdk fjas;ldkfj a;slkdfj ;lakasdlk;jkdj }                                                                               \\ \hline
Fully Met                                  & Met                                      & Partially Met                                       & Not Met                                      \\ \hline
Comments:                                  & \multicolumn{3}{l|}{}                                                                                                                         \\ \hline
\multicolumn{4}{|p{\textwidth}|}{a;lsdkjf; lakj;l3: alkjdsf l;aksjdf;l kasjd;lf kjasd;l fkjas;ldkfjas;ldkf jas;ldkf ja;sldkfj asldkfj alksjferopijp34qjp asdf} \\ \hline
Fully Met                                  & Met                                      & Partially Met                                       & Not Met                                      \\ \hline
Comments:                                  & \multicolumn{3}{l|}{}                                                                                                                         \\ \hline
\multicolumn{4}{|p{\textwidth}|}{as;ldkfjal;kej ;l4: alskdjf ;lasdkjf ;alskdjf ;laskd}                                                                                                                      \\ \hline
Fully Met                                  & Met                                      & Partially Met                                       & Not Met                                      \\ \hline
Comments:                                  & \multicolumn{3}{l|}{}                                                                                                                         \\ \hline
\multicolumn{4}{|p{\textwidth}|}{asl;dkjf;lk4j: asl;dkjfaslkdjf eirkajp (asl;dkfj a) aslkdjf l}                                                                                             \\ \hline
Fully Met                                  & Met                                      & Partially Met                                       & Not Met                                      \\ \hline
Comments:                                  & \multicolumn{3}{l|}{}                                                                                                                         \\ \hline
\end{tabularx}

Gives:

Gives this

3

(rewrote parts of the answer to incorporate @Zarko's comments and observations)

Your column-width calculations aren't taking into account that space needs to be set aside for (a) the whitespace padding around columns and (b) the widths of the vertical lines. You need to

  • replace all instances of

    \multicolumn{2}{|p{0.5\textwidth}|}
    

    with

    \multicolumn{2}{|P\lengtha}|}
    
  • and replace all instances of

    \multicolumn{4}{|p{\textwidth}|}
    

    with

    \multicolumn{4}{|P{\lengthb}|}
    

where \lengtha and \lengthb are defined in the code shown below. P is defined via the instruction

\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}

The \RaggedRight instruction, provided by the ragged2e package, serves to typeset the material in ragged-right moder rather in fully-justified mode, while still permitting hyphenation. (Because the columns are quite narrow, it is much more difficult to fully justify the column text satisfactorily. Better to go to ragged-right mode immediately.)

Furthermore, instructions such as

\multicolumn{2}{l|}{as;ldifja;lsdkfja;lsdkfja; slkfj; aldkjf ;aslkdj ;alk}

seriously mess up the appearance of the table because the l column type doesn't allow automatic line breaking. You should either have only short strings in columns that don't allow line breaking or not use the l column type to begin with. My recommendation is to replace all instances of \multicolumn{2}{l|} with \multicolumn{2}{P{\lengtha}|}.

An additional note about the code: I've also defined a variant of the X column type, called Y, that typesets its material in ragged-right rather than in fully-justified mode. The Y column type is used for the main column definitions.

enter image description here

\documentclass{article}
\usepackage{tabularx,ragged2e} 
% "\RaggedRight" allows hyphenation
\newcolumntype{P}[1]{>{\RaggedRight\arraybackslash}p{#1}}
\newcolumntype{Y}{>{\RaggedRight\arraybackslash}X} 

\newlength\lengthx % basic column width
\setlength{\lengthx}{\dimexpr0.25\textwidth-2\tabcolsep-1.25\arrayrulewidth\relax}
\newlength\lengtha % width of two-columm multicolumn
\setlength{\lengtha}{\dimexpr2\lengthx+2\tabcolsep+1\arrayrulewidth\relax}
\newlength\lengthb % width of four-columm multicolumn
\setlength{\lengthb}{\dimexpr\textwidth-2\tabcolsep-2\arrayrulewidth\relax}

\begin{document}
\noindent % <--- this is necessary
\begingroup
\setlength\extrarowheight{2pt} % optional: go for a more open "look"
\begin{tabularx}{\textwidth}{|Y|Y|Y|Y|}
\hline
Name: & XYZ & Student ID: & 99999999 
\\ \hline
\multicolumn{2}{|P{\lengtha}|}{Feedback focus:} &
\multicolumn{2}{ P{\lengtha}|}{asoidjfaopsidjfp oiajdfpo iasjdfp oiasjdfpo ij} 
\\ \hline
\multicolumn{2}{|P{\lengtha}|}{askdljf ;laksjdf;l aksjdf;lkajsd;:} & 
\multicolumn{2}{ P{\lengtha}|}{as;ldifja;lsdkfja;lsdkfja; slkfj; aldkjf ;aslkdj ;alk}
\\ \hline \hline
\multicolumn{4}{|P{\lengthb}|}{as;ldkf;lkj: aklsduhf asl dkj f;als kdjf ;als kdjf ;alskdf } 
\\ \hline
Fully Met & Met & Partially Met & Not Met 
\\ \hline
Comments: & \multicolumn{3}{l|}{} 
\\ \hline \hline
\multicolumn{4}{|P{\lengthb}|}{asldkjf;alksdj f;lak sjdf:as;dlk ja;lsdk fjas;ldkfj a;s lkdfj ;lak asdlk;jkdj }            
\\ \hline
Fully Met & Met & Partially Met & Not Met 
\\ \hline
Comments: & \multicolumn{3}{l|}{}     
\\ \hline \hline
\multicolumn{4}{|P{\lengthb}|}{a;lsdkjf; lakj;l3: alkjdsf l;ak sjdf;l ka sjd;lf kjasd;l fkjas;ld kfjas; ldkf jas ;ldkf ja;s ldkfj asl dkfj alk sjfe ropij p34qjp asdf} 
\\ \hline
Fully Met & Met & Partially Met & Not Met
\\ \hline
Comments: & \multicolumn{3}{l|}{} 
\\ \hline \hline
\multicolumn{4}{|P{\lengthb}|}{as;ldkfjal;kej ;l4: alskdjf ;lasdkjf ;alskdjf ;laskd} 
\\ \hline
Fully Met & Met & Partially Met & Not Met
\\ \hline
Comments: & \multicolumn{3}{l|}{}
\\ \hline \hline
\multicolumn{4}{|P{\lengthb}|}{asl;dkjf;lk4j: asl;dkjfaslkdjf eirkajp (asl;dkfj a) aslkdjf l}
\\ \hline
Fully Met & Met & Partially Met & Not Met 
\\ \hline
Comments: & \multicolumn{3}{l|}{}
\\ \hline
\end{tabularx}
\endgroup
\end{document}
  • Excellent, thank you! I'll use this from now... – lmsavk Dec 4 '16 at 16:56
  • In tests of your solution I was faced with unexpected phenomena: text in multi column cells doesn't exploit all cell width (minus tabcolsep, rather end earlier for about x tabcolsep distances, where x is number columns spanned with multicolumn. Do you observe the same? Can you add to your answer an example of use of your suggested solution? – Zarko Dec 4 '16 at 20:12
  • More accurate measurement of distance of the right column text border from end of cell show, that the x is number of tabcolsep spanned with multicolumn cell minus 2. Full width of cell I was able to exploit with the following solution: \multicolumn{4}{|>{\hsize=4\hsize}X|}{\begin{minipage}{\dimexpr4\linewidth+6\tabcolsep\relax} xxx xxxx xx x xxx xxxx xx xxxx xxxx xx xxxx xxxx xx xxxx xxxx xx xxxx x x x x x x x xxx xxxx xx xxxx xxxx xx xxxx xxxx xx xxxx xxxx xx xxxx xxxx xx x xxx xxxx xx x \end{minipage}} \\ \hline. – Zarko Dec 4 '16 at 20:24
  • Yes, I did. Tos see what I observe, please use example (without mnipage) where is nothing to hyphenate, but text still not fill all cell width. – Zarko Dec 4 '16 at 20:42
  • @Zarko - Thanks for your comments! I've rewritten parts of the answer and fully updated the code. The screenshot shows that the widths of the columns are now fully used. – Mico Dec 4 '16 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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