Latex Tables – How to Set Font Size

fontsizetables

I have the following table with column linewidth. I want to set the font size of the table to some specific value. Several suggestions say that a code like p{0.4in} will work. I tried p{0.18\linewidth}{0.4in} but it is not working. How to fix this? Thanks in advance.

\begin{table}[H]
\centering
\setlength{\tabcolsep}{0.2em} % for the horizontal padding
\caption{A table}
\label{tab:42}
\resizebox{\textwidth}{!}{%
    \begin{tabular}{ |p{0.18\linewidth} | p{0.20\linewidth} |}
        %Head row starts************
        \hline
        Column1 & Column2 \\
        \hline
        Data1 & Data2 \\
        \hline
    \end{tabular}%
}
\end{table}

Best Answer

As @DavidCarlisle has already pointed out in a comment, nobody should ever employ \resizebox to force the tabular-like material to occupy a given width, say, \linewidth. Instead, use a tabularx or a tabular* environment to set the overall width. That way, you'll avoid getting wildly inconsistent font sizes across tables.

If you must still adjust the font size, you may use one of LaTeX's many font size changing macros. The following screenshot shows the effects of employing \Large, \large, \normalsize (the default), \small, \footnotesize, and \scriptsize.

enter image description here

\documentclass{article}
\usepackage{tabularx}
\setlength\extrarowheight{2pt}
\begin{document}

\begin{table}[ht!]
\caption{A full-width \texttt{tabularx} environment at various font sizes\strut} \label{tab:42}

\Large
    \begin{tabularx}\linewidth{ | X | X  |}
        \hline
        Column1 & Column2 \\
        \hline
        Data & \texttt{\textbackslash Large} \\
        \hline
    \end{tabularx}
\medskip

\large
    \begin{tabularx}\linewidth{ | X | X  |}
        \hline
        Column1 & Column2 \\
        \hline
        Data & \texttt{\textbackslash large} \\
        \hline
    \end{tabularx}
\medskip

\normalsize % that's the default
    \begin{tabularx}\linewidth{ | X | X  |}
        \hline
        Column1 & Column2 \\
        \hline
        Data & \texttt{\textbackslash normalsize} \\
        \hline
    \end{tabularx}
\medskip

\small
    \begin{tabularx}\linewidth{ | X | X  |}
        \hline
        Column1 & Column2 \\
        \hline
        Data & \texttt{\textbackslash small} \\
        \hline
    \end{tabularx}
\medskip

\footnotesize
    \begin{tabularx}\linewidth{ | X | X  |}
        \hline
        Column1 & Column2 \\
        \hline
        Data & \texttt{\textbackslash footnotesize} \\
        \hline
    \end{tabularx}
\medskip

\scriptsize
    \begin{tabularx}\linewidth{ | X | X  |}
        \hline
        Column1 & Column2 \\
        \hline
        Data & \texttt{\textbackslash scriptsize} \\
        \hline
    \end{tabularx}
\end{table}
\end{document}
Related Question