表格文字换行

表格文字换行

我正在尝试获取这样的表格,但我得到的输出不能让我满意。你知道我该如何更改此代码吗?

这是我想要获取的表格:

在此处输入图片描述

这是我获得的表格:

在此处输入图片描述

您可以看到,在开头,例如,“无代谢影响”和“感染(不高于背景人群)”与“铜”不一致。

这是 MWE:

\documentclass[12pt]{article}
\usepackage[left=35mm, right=25mm, top=30mm, bottom=30mm]{geometry}
\usepackage{graphicx} 
\usepackage{multirow}

\begin{document}   


\begin{table}[htbp]
\footnotesize
\linespread{1}\selectfont
    \caption{Schematic overview of the different available contraceptive methods, and pro and cons for their use in women with diabetes}
    \begin{center}
        \begin{tabular}{l l l l l}
                \toprule
                & \multirow{2}{2cm}{\textit{Methods}} & \multicolumn{2}{c}{\textit{Type 1 diabetes}}\\
                \cmidrule(l){3-4}
                & & \textit{Pro} & \textit{Con}\\
                \midrule
                \multicolumn{2}{l}{Barrier} & No metabolic effects & \parbox{5cm}{Compliance difficult\\Medium contraceptive efficacy}\\
                \multicolumn{2}{l}{IUD} & & \\
                & Copper & \parbox{5cm}{No metabolic effects\\Good compliance\\High contraceptive efficacy}& \parbox{5cm}{Infection (not higher than in background population)\\Bleeding disturbance}\\
                & Progestin containing & \parbox{5cm}{Decreased menstrual bleeding (Progestin-IUD)}& \\
                \multicolumn{2}{l}{Oral contraceptives} & \parbox{5cm}{Good bleeding control\\High contraceptive efficacy}& \parbox{5cm}{Risk of arterial thrombotic episode (higher than background population)\\Risk of venous thrombotic episode (as background population)\\Limited use in non-smoking women \textless{} 35 years with uncomplicated diabetes\\Increasing insulin resistance\\Decreasing glucose tolerance (not clinically relevant)}\\
                \multicolumn{2}{l}{Progestin-only pills} & \parbox{5cm}{No increased risk of vascular disease\\Medium-high contraceptive efficacy}& \parbox{5cm}{Non-optimal bleeding control\\Need to be taken at the same time everyday}\\
                \multicolumn{2}{l}{Long-acting hormonal methods} & High contraceptive efficacy & \parbox{5cm}{Non-optimal bleeding control\\Not been studied in women with diabetes}\\
                \multicolumn{2}{l}{Surgical sterilization} & & \\
                & Female & No metabolic effects & Irreversible\\
                & Male & High contraceptive efficacy & Need of an operation\\
                \bottomrule
        \end{tabular}
    \end{center}
    \label{tab:contraception_options}
\end{table}

\begin{document}

答案1

为了提高可读性,我建议采用这种布局:我在方便的list环境中定义两种列类型和内容。ProCon单元格中的每一行都必须以 开头\item。 我还加载了包以获得更好的格式,并将添加了不需要的垂直间距的环境caption替换为:center\centering

\documentclass[12pt]{article}
\usepackage[left=35mm, right=25mm, top=30mm, bottom=30mm]{geometry}
\usepackage{graphicx}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{caption} \usepackage{enumitem}
\usepackage{ragged2e}
    \newlist{tabenum}{itemize}{1}
    \setlist[tabenum]{label={}, labelsep=0pt, wide=0pt, nosep, leftmargin=1.5em, itemsep=2pt, before =\vspace*{-\dimexpr\baselineskip+\itemsep\relax}}%
    \newcolumntype{P}{>{\RaggedRight\tabenum\item[]}p{50mm}<{\endtabenum\vspace*{-2ex}}}%
    \newcolumntype{Q}{>{\RaggedRight\tabenum\item[]}X<{\endtabenum\vspace*{-2ex}}}

\begin{document}

\begin{table}[htbp]
\footnotesize\centering\setlength\tabcolsep{4pt}
\linespread{1}\captionsetup{format=hang}
    \caption{Schematic overview of the different available contraceptive methods,\\ and pro and cons for their use in women with diabetes}
        \begin{tabularx}{\linewidth}{@{}llPQ@{}}
                \toprule
                & \multirow{2}{2cm}{\textit{Methods}} & \multicolumn{2}{c}{\textit{Type 1 diabetes}}\\
                \cmidrule(l){3-4}
                & & \multicolumn{1}{l}{\itshape Pro} & \multicolumn{1}{l}{\itshape Con}\\
                \midrule
\addlinespace
                \multicolumn{2}{l}{Barrier} & \item No metabolic effects & \item Compliance difficult \item Medium contraceptive efficacy\\
                \addlinespace
                \multicolumn{2}{l}{IUD} & & \\
                & Copper & \item No metabolic effects \item Good compliance \item High contraceptive efficacy %
                & \item Infection (not higher than in background population) \item Bleeding disturbance\\
                \addlinespace
             & Progestin containing & \item Decreased menstrual bleeding (Progestin-IUD)& \\
 \addlinespace
 \multicolumn{2}{l}{Oral contraceptives}%
  & \item Good bleeding control \item High contraceptive efficacy %
  & \item Risk of arterial thrombotic episode (higher than background population) \item Risk of venous thrombotic episode (as background population) \item Limited use in non-smoking women \textless{} 35 years with uncomplicated diabetes \item Increasing insulin resistance \item Decreasing glucose tolerance (not clinically relevant)\\
 \addlinespace
 \multicolumn{2}{l}{Progestin-only pills}%
  & \item No increased risk of vascular disease \item Medium-high contraceptive efficacy %
  & \item Non-optimal bleeding control \item Need to be taken at the same time everyday \\
 \addlinespace
 \multicolumn{2}{p{3.5cm}}{Long-acting hormonal methods}%
  & \item High contraceptive efficacy %
  & \item Non-optimal bleeding control \item Not been studied in women with diabetes\\
 \multicolumn{2}{l}{Surgical sterilization} & & \\
 &Female & \item No metabolic effects & \item Irreversible\\
 & Male & \item High contraceptive efficacy & \item Need of an operation\\
 \bottomrule
        \end{tabularx}
    \label{tab:contraception_options}
\end{table}

\end{document} 

在此处输入图片描述

答案2

这里的问题在于您对 parboxes 的使用。当然,您也可以使用可选参数中的 add 来解决这个问题(\parbox[pos][height][contentpos]{width}{text},其中pos应设置为t,以使内容顶部对齐。)。但这可以使用不同的列类型来解决,即p{width},因为无论如何您都在所有这些 parboxes 中设置了相同的宽度。那么您不需要一遍又一遍地写相同的内容。现在您可以使用手动换行符,但您必须使用,\newline因为\\在表格环境(垂直模式)中具有不同的含义。

我还建议Ragged2e在这里使用 -package,因为它比简单的 更擅长处理换行符\raggedright,在我看来,当列非常小的时候,它看起来更好。此外,它还有助于使表格适合文本。我现在已经添加了这些选项,但它们很容易删除。我还通过-commandtabularx在几行选定的行之间添加了一些空格,以使信息更清晰一些。booktabs\addlinespace

输出

在此处输入图片描述

代码

\documentclass[12pt]{article}
\usepackage[left=35mm, right=25mm, top=30mm, bottom=30mm]{geometry}
\usepackage{graphicx} 
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{tabularx}
\usepackage{ragged2e}

\begin{document}   


\begin{table}[htbp]
\footnotesize
\RaggedRight
\linespread{1}\selectfont
    \caption{Schematic overview of the different available contraceptive\newline methods, and pro and cons for their use in women with diabetes}
    \begin{center}
        \begin{tabularx}{\textwidth}{l l p{5cm} X}
                \toprule
                & \multirow{2}{2cm}{\textit{Methods}} & \multicolumn{2}{c}{\textit{Type 1 diabetes}}\\
                \cmidrule(l){3-4}
                & & \textit{Pro} & \textit{Con}\\
                \midrule
                \multicolumn{2}{l}{Barrier} & No metabolic effects & \parbox{5cm}{Compliance difficult\\Medium contraceptive efficacy}\\
                \addlinespace
                \multicolumn{2}{l}{IUD} & & \\
                & Copper & No metabolic effects\newline Good compliance\newline High contraceptive efficacy& Infection (not higher than in background population)\newline Bleeding disturbance\\
                \addlinespace
                & Progestin containing & Decreased menstrual bleeding (Progestin-IUD)& \\
                \addlinespace
                \multicolumn{2}{l}{Oral contraceptives} & Good bleeding control\newline High contraceptive efficacy& Risk of arterial thrombotic episode (higher than background population)\newline Risk of venous thrombotic episode (as background population)\newline Limited use in non-smoking women \textless{} 35 years with uncomplicated diabetes\newline Increasing insulin resistance\newline Decreasing glucose tolerance (not clinically relevant)\\
                \addlinespace
                \multicolumn{2}{l}{Progestin-only pills} & No increased risk of vascular disease\newline Medium-high contraceptive efficacy& Non-optimal bleeding control\newline Need to be taken at the same time everyday\\
                \addlinespace
                \multicolumn{2}{p{3.5cm}}{Long-acting hormonal methods} & High contraceptive efficacy & Non-optimal bleeding control\newline Not been studied in women with diabetes\\
                \multicolumn{2}{l}{Surgical sterilization} & & \\
                & Female & No metabolic effects & Irreversible\\
                & Male & High contraceptive efficacy & Need of an operation\\
                \bottomrule
        \end{tabularx}
    \end{center}
    \label{tab:contraception_options}
\end{table}

\end{document}

相关内容