0

I am trying to make table as wide as the text, but I can't figure it out. I used \usepackage[pass,letterpaper]{geometry} to make left and right margin equal. Also, I would like to center text in the table. How I can do that?

enter image description here

\begin{table}[h!]
\begin{tabular}{|p{0.5\textwidth}|p{0.5\textwidth}|}
\hline
Adres dynamiczny & Adres statyczny \\
\hline
Jest zmienny & Jest stały \\
\hline
Gdy host loguje się do sieci, otrzymuje przypadkowe IP (najczęściej pierwsze wolne) & Gdy host loguje się do sieci, otrzymuje zawsze jednakowe IP \\
\hline
\end{tabular}
\caption{\label{tab:Adresy}Porównanie adresu dynamicznego ze statycznym}
\end{table}
1
  • 1
    Welcome to TeX SX! You forgot to take into account the value of \tabcolsep. Loading array, you can use \wc{\dimexpr 0.5\textwidth-2\tabcolsep} instead of p{0.5\textwidth}. Other solution, with tabularx: \begin{tabularx}{\textwidth}{>{\centering}X>{\centering}X}. – Bernard Mar 28 at 17:52
2

Width in p column type specify width of text in cell, not distance between cell borders. SO you need to reduce width for two tabcolsep and for one and half of \arrayrulewidth of vertical lines between columns:

Edit: Now contents of cells are centered. Also is corrected calculation of the columns widths.

\documentclass{article}
\usepackage{array}  % <---


%---------------- show page layout. don't use in a real document!
\usepackage{showframe}
\renewcommand\ShowFrameLinethickness{0.15pt}
\renewcommand*\ShowFrameColor{\color{red}}
%---------------------------------------------------------------%

\begin{document}
    \begin{table}[ht]
    \centering
\begin{tabular}{|*{2}{>{\centering\arraybackslash}p{\dimexpr0.5\textwidth-2\tabcolsep-1.5\arrayrulewidth}|}}
\hline
Adres dynamiczny    & Adres statyczny \\
    \hline
Jest zmienny        & Jest stały \\
    \hline
Gdy host loguje się do sieci, otrzymuje przypadkowe IP (najczęściej pierwsze wolne) 
                    & Gdy host loguje się do sieci, otrzymuje zawsze jednakowe IP \\
    \hline
\end{tabular}
\caption{Porównanie adresu dynamicznego ze statycznym}
    \label{tab:Adresy}
\end{table}
\end{document}

enter image description here

5
  • Thank you so much, it works perfectly! Although \centering doesn't seem to do much. What should it do? I would like to center text in every cell. – Nicolaus Mar 28 at 18:14
  • @Zarko: Shouldn't it be -1.5\arrayrulewidth instead of just -0.5\arrayrulewidth? – leandriis Mar 28 at 18:22
  • @Nicolaus: To horizontally center the contents, you can use >{\centering\arraybackslash} as in \begin{tabular}{|*{2}{>{\centering\arraybackslash}p{\dimexpr0.5\textwidth-2\tabcolsep-1.5\arrayrulewidth}|}}. – leandriis Mar 28 at 18:22
  • @Nicolaus, \centering center table in the float. See edited answer. – Zarko Mar 28 at 19:38
  • @leandriis, you are right. in writing od answer I lost one vertical lines widths :-(. Now corrected. – Zarko Mar 28 at 19:38
0

I have tried to produce an MWE guessed on your code, which has some errors as it doesn't use package that you use in your code. Please in the future provide an MWE (from documentclass... to \end{document).

In a tabular the vertical lines take up some horizontal space so allowance has to be made for them. Reduce the width of the tabular columns (someone else might tell you the exact amount) but the following works.

% tablefitprob.tex  SE 590404

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{table}[h!]
\centering
\begin{tabular}{|p{0.45\textwidth}|p{0.45\textwidth}|}
\hline
Adres dynamiczny & Adres statyczny \\
\hline
Jest zmienny & Jest stały \\
\hline
Gdy host loguje się do sieci, otrzymuje przypadkowe IP (najczęściej pierwsze wolne) & Gdy host loguje się do sieci, otrzymuje zawsze jednakowe IP \\
\hline
\end{tabular}
\caption{\label{tab:Adresy}Porównanie adresu dynamicznego ze statycznym}
\end{table}
\lipsum[1]
\end{document}

Again, someone else might help with centering of the entries.

2
  • Your code currently isn't compilable. You might want to replace \end{tabular*} with \end{tabular}. – leandriis Mar 28 at 18:57
  • @leandriis. Thank you. Done. – Peter Wilson Mar 29 at 18:11

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.