เราจะสร้างตารางที่ดูดีใน Latex ได้อย่างไร?

Aug 20 2020

ฉันสามารถสร้างตารางที่ดูอ่อนโยน ฉันสงสัยว่าจะสร้างตารางได้อย่างไรด้านล่าง:

\begin{table}[htbp]
\caption{Variable Descriptions}
\label{tab:2}
\begin{center}
\begin{tabular}{|c|c|}\hline
Variables & Descriptions\\\hline
\textit{ln(wage)} & log of wage\\
\textit{educ} & years of education\\
\textit{black} & 1 if black and 0 if not\\
\textit{hisp} & 1 if hispanic and 0 if not\\
\textit{exper} & years of experience\\
\textit{exper$^{2}$} & years of experience squared\\
\textit{married} & 1 if married and 0 if not\\
\textit{union} & 1 if belongs to a union and 0 if not\\\hline
\end{tabular}
\end{center}
\end{table}

คำถามเฉพาะคือ:

  • หนึ่งแถวสลับสีระหว่างสีเทาและสีขาวได้อย่างไร? มีวิธีสร้างคำสั่งแทนการระบายสีด้วยตนเองหรือไม่?

คำตอบ

6 leandriis Aug 20 2020 at 03:37

ตารางของคุณมีสามเวอร์ชันดังนี้

\documentclass{article}
\usepackage[table,svgnames]{xcolor} % provides the \rowcolors command 
\usepackage{caption} % for improved spacing around the caption

\usepackage{array} % enables >{...} in the coumn specifier section, used in table 2 & 3
\usepackage{booktabs} % for improved spacing around horizontal lines, used in example 3, incompatible with vertical lines, be careful if you want to to combine it with color

\usepackage[column=0]{cellspace} % for adding a small amount of space above and below each cell, only used in table 2
\setlength{\cellspacetoplimit}{2pt}
\setlength{\cellspacebottomlimit}{\cellspacetoplimit}


\begin{document}
\begin{table}[htbp]
\caption{Variable Descriptions}
\label{tab:2}
\centering % used \centering instead of the center environment since the latter adds additional white space
\rowcolors{2}{white}{lightgray} 
\begin{tabular}{|c|c|}\hline
Variables & Descriptions\\\hline
\textit{ln(wage)} & log of wage\\
\textit{educ} & years of education\\
\textit{black} & 1 if black and 0 if not\\
\textit{hisp} & 1 if hispanic and 0 if not\\
\textit{exper} & years of experience\\
\textit{exper$^{2}$} & years of experience squared\\
\textit{married} & 1 if married and 0 if not\\
\textit{union} & 1 if belongs to a union and 0 if not\\\hline
\end{tabular}
\end{table}

\begin{table}[htbp]
\caption{Variable Descriptions}
\label{tab:2}
\centering
\rowcolors{2}{white}{lightgray} 
\begin{tabular}{>{\itshape}0l0l}\hline % used >{\itshape} in order to be able to remove the repeated occurences of \textit in the first column, used l type columns instead of c columns for a cleaner look, added small vertical space above and below the rows with the help of the cellspace package, removed all vertical lines
\textup{Variables}       & Descriptions\\\hline
ln(wage)                 & log of wage\\
educ                     & years of education\\
black                    & 1 if black and 0 if not\\
hisp                     & 1 if hispanic and 0 if not\\
exper                    & years of experience\\
exper\textsuperscript{2} & years of experience squared\\
married                  & 1 if married and 0 if not\\
union                    & 1 if belongs to a union and 0 if not\\\hline
\end{tabular}
\end{table}

\begin{table}[htbp]
\caption{Variable Descriptions}
\label{tab:2}
\centering
\begin{tabular}{>{\itshape}ll}
\toprule % replaced all \hline commands with rules from the booktabs package
\textup{Variables}       & Descriptions\\
\midrule
ln(wage)                 & log of wage\\
educ                     & years of education\\
black                    & 1 if black and 0 if not\\
hisp                     & 1 if hispanic and 0 if not\\
exper                    & years of experience\\
exper\textsuperscript{2} & years of experience squared\\
married                  & 1 if married and 0 if not\\
union                    & 1 if belongs to a union and 0 if not\\
\bottomrule
\end{tabular}
\end{table}
\end{document}
7 SimonDispa Aug 20 2020 at 21:08

ฉันจะตอบคำถามหลักของคุณ: I am wondering how to create tables like this.

ฉันทำงานกับตารางตลอดเวลาตั้งแต่ผลการทดสอบไปจนถึงงบประมาณรายเดือน สำหรับงานต่างๆเช่นการเรียงลำดับการจัดรูปแบบการตรวจสอบและการวิเคราะห์ข้อมูลอย่างง่ายฉันทำใน Excel (โดยใช้ 2013) ในที่สุดตารางสุดท้ายจะออกมาหลังจากการทำซ้ำหลายครั้งและเวลา

ในตัวอย่างของคุณเป็นที่ชัดเจนว่าข้อมูลมาจากฐานข้อมูลจะมีผลการสะสมตามแนวตั้งของแพตช์ทั้งหมดและการแก้ไขทั้งหมดการตรวจสอบความสอดคล้องกันเป็นต้น

LaTeX มีไว้สำหรับเผยแพร่รายงานที่สวยงาม แต่จะเกิดขึ้นปีละสองสามครั้ง เฉพาะในตารางธรรมดาเท่านั้นที่สามารถเขียนข้อมูลไปยัง LaTeX ได้โดยตรง

เวิร์กโฟลว์ Excel ไปยัง LaTeX ของฉันมี 3 ขั้นตอน:

1 จัดรูปแบบตารางสุดท้ายด้วยสไตล์ที่คุณต้องการใน Excel ใช้สีไม้บรรทัด ฯลฯ ตามความจำเป็น

  1. ส่งออกตารางโดยใช้ Add-in ของ Excel2Latex ของ CTAN ติดตั้งง่ายและใช้งานได้ตามโฆษณา

https://ctan.org/tex-archive/support/excel2latex?lang=en

คัดลอกไปที่คลิปบอร์ดและวางลงในเอกสาร LaTeX พื้นฐาน

คุณต้องเพิ่มแพ็คเกจพื้นฐาน (ดูตัวอย่างของฉัน) และเรียกใช้รหัสผลลัพธ์

คุณจะเห็นว่าคุณสมบัติส่วนใหญ่เช่นหลายแถวสีและที่สำคัญที่สุดคือส่งผ่านเนื้อหาไปยัง LaTeX เรียบร้อยแล้ว หากคุณเปลี่ยนใจเกี่ยวกับรายละเอียดในสไตล์คุณสามารถแก้ไขแผ่นงาน Excel และสร้างโค้ด LaTeX ใหม่ได้อย่างง่ายดาย

3 ใช้การตกแต่งขั้นสุดท้าย: แบบอักษรยืดยัติภังค์สัญลักษณ์ ฯลฯ โดยใช้คำสั่ง LaTeX

สำหรับตัวอย่างของคุณฉันรวมcaptionแพ็กเกจเพื่อให้ตรงกับคำอธิบายภาพต้นฉบับเพิ่มตัวยกในแถวแรกขีดเส้นใต้ในแถวที่สองและตัวพิมพ์เล็กตามต้องการ ฉันใช้arraystretchจาก 1.3 เพื่อขยายเซลล์ในแนวตั้ง

ในกรณีนี้ฉันต้องรวมจุดชดเชย 7 จุดลงในแถวสีเทาคู่เพื่อป้องกันไม่ให้พื้นหลังของเซลล์ด้านล่างเขียนทับส่วนหนึ่งของเนื้อหาเซลล์ นี่เป็นเหตุการณ์ปกติเมื่อทำงานกับเซลล์สี แสดงความคิดเห็น[7pt]และดูว่าเกิดอะไรขึ้น

แน่นอนว่าทุกอย่างจะออกมาดูดีถ้าคุณใช้สีพื้นหลังเพียงสีเดียว! แต่ฉันพบว่ารูปแบบสีของแถวทางเลือกนั้นดีมากและจำเป็นต้องมีตารางหลายคอลัมน์ที่กว้าง การนำเสนอของ Beamer จะถึงวาระหากคุณใช้เฉพาะขาวดำ

\documentclass[12pt,a4paper]{book}

\RequirePackage[table]{xcolor}
\RequirePackage{booktabs}
\RequirePackage{multirow}

\RequirePackage{caption}% typesetting of captions
\captionsetup{%
    labelfont={bf}, %Table 1
    textfont={bf}  %caption font
}%

\begin{document}

% Table generated by Excel2LaTeX from sheet 'Sheet1'
\begin{table}[htbp]
    \renewcommand{\arraystretch}{1.3} % expand the rows vertically
    \setlength\belowcaptionskip{8pt}    
    \centering
    \caption{Test--suite based program repair tools for Java.}
    \begin{tabular}{llrrr}
        \toprule
        \multicolumn{1}{c}{\multirow{2}[2]{*}{Repear tool}} & Benchmark used & \multicolumn{1}{r}{\multirow{2}[2]{*}{\# Bugs}} & \multirow{2}[2]{*}{\# Patched\textsuperscript{a}} & \multirow{2}[2]{*}{\# Fixed\textsuperscript{b}} \\
        & in evaluation &       &       &  \\
        \midrule
        \multicolumn{5}{l}{\underline{\emph{Generate--and--validate}}} \\
        \rowcolor[rgb]{ .906,  .902,  .902} ACS [4] & Defects4J & 224   & 23    & 17 \\
        \multirow{2}[0]{*}{ARJA [50]} & Defects4J & 224   & 59    & 18 \\
        & QuixBugs [48] & 40    & 4     & 2 \\
        \rowcolor[rgb]{ .906,  .902,  .902} \multirow{2}[1]{*}{\textsc{CapGen} [42]}  & Defects4J & 224   & 25    & 22 \\[7pt] % shift needed for colored multirows
        \rowcolor[rgb]{ .906,  .902,  .902}     & IntroClassJava & 297   & --    & 25 \\
        \multirow{2}[0]{*}{Cardumen [28]} & Defects4J & 356   & 77    & -- \\
        & QuixBugs [48] & 40    & 5     & 3 \\
        \rowcolor[rgb]{ .906,  .902,  .902} DeepRepair [43] & Defects4J & 374   & 51    & -- \\
        \multirow{2}[0]{*}{\textsc{Elixir} [37]} & Defects4J & 82    & 41    & 26 \\
        & Bugs.jar & 127   & 39    & 22 \\
        \rowcolor[rgb]{ .906,  .902,  .902} GenProg-A & Defects4J & 224   & 36    & -- \\
        HDRepair [18] & Defects4J & 90    & --    & 23 \\
        \rowcolor[rgb]{ .906,  .902,  .902}\textsc{Jaid} [2] & Defects4J & 138   & 31    & 25 \\
        \multirow{3}[0]{*}{jGenProg [27]} & Defects4J & 224   & 29    & -- \\
        & Defects4J [26] & 224   & 27    & 5 \\
        & QuixBugs [48] & 40    & 2     & 0 \\
        \ldots  & \ldots & \ldots    & \ldots    & \ldots \\ % more rows follows
        \bottomrule
        \multicolumn{5}{l}{\footnotesize \textsuperscript{a} As reported last week. \ \textsuperscript{b} Verified by QC. }\\
\end{tabular}%
\label{tab:addlabel}%
\end{table}%

\end{document}

นี่คือผลลัพธ์สุดท้าย เวลาทั้งหมด 1-2 ชั่วโมงส่วนใหญ่อยู่ในการป้อนข้อมูลและจัดรูปแบบแผ่นงาน

ในแบบฝึกหัดสุดท้ายให้ลองเพิ่มคอลัมน์อื่นในตารางที่เจ้านายของคุณถามก่อนกำหนดเวลา: Still Missingด้วยข้อมูล (คำนวณในแผ่นงาน excel) ที่มีค่าBugsลบPatchesลบFixedและดูว่าทั้งหมดทำงานอย่างไร (และใช้เวลาเพียงเล็กน้อย!) . สถานการณ์ในโลกแห่งความเป็นจริง

5 F.Pantigny Aug 20 2020 at 18:33

หมายเหตุ: คำตอบนี้ต้องการรุ่น 5.2 (2020/08/19) nicematrixของ

ภาพที่กำหนดโดย OP จะแสดงแถวที่มีสีสลับกัน แต่ในความเป็นจริงบางแถวคือการรวมตัวกันของหลายแถว ในสภาพนี้มันเป็นเรื่องยากที่จะใช้ของ\rowcolors xcolorในเวอร์ชันล่าสุดของnicematrix(ข้อ 5.2 ของ 2020-08-19) คำสั่งในตัว\rowcolors(ที่จะใช้code-before) มีตัวเลือกrespect-blocksในการจัดการกับปัญหานั้น แถวที่ล้อมรอบบล็อกถือเป็นแถวเดียว

\documentclass{article}
\usepackage{nicematrix}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{makecell}
\usepackage{enumitem}

\NiceMatrixOptions{notes = { para , style = \alph{#1} } }

\begin{document}

\begin{NiceTabular}{ll@{\space}lrrr}%
 [ code-before = \rowcolors{3}{gray!20}{}[respect-blocks] ]
\toprule
Repair tool & 
\multicolumn{2}{l}{\makecell[l]{Benchmark used\\ in evaluation}} 
& 
\# Bugs &
\# Patched\tabularnote{Note}& 
\# Fixed\tabularnote{Note}\\
\midrule
\underline{\emph{Generate-and-validate}} \\
ACS [46] & Defects4J & & 224 & 23 & 17 \\
\Block{2-1}{}\multirow{2}{*}{ARJA [50]} & Defects4J&  & 224 & 59 & 18 \\
& QuixBugs& [38] & 40 & 4 & 2 \\
\Block{2-1}{}\multirow{2}{*}{\textsc{CapGen} [42]} & Defects4J& & 224 & 25 & 22 \\
& \rlap{IntroClassJava} & & 297 & -- & 25 \\
\Block{2-1}{}\multirow{2}{*}{Cardumen [28]} & Defects4J& & 356 & 77 & -- \\
& QuixBugs& [48] & 40 & 5 & 3 \\
DeepRepair [43] & Defects4J& & 374 & 51 & - \\
\Block{2-1}{}\multirow{2}{*}{\textsc{Elixir} [37]} & Defects4J& & 82 & 41 & 26 \\
& Bugs.jar & & 127 & 39 & 22 \\
GenProg-1 [50] & Defects4J& & 224 & 36 & -- \\
HDRepair [18] & Defects4J& & 90 & -- & 23 \\
\textsc{Jaid} [2] & Defects4J& & 138 & 31 & 25 \\
\Block{3-1}{}\multirow{3}{*}{jGenProg [27]} & Defects4J & & 224 & 29 & -- \\
& Defects4J & [26] & 224 & 27 & 5 \\
& QuixBugs& [48] & 40 & 2 & 0 \\
\Block{3-1}{}\multirow{3}{*}{jKali [27]} & Defects4J& & 224 & 22 & -- \\
& Defects4J& [26] & 224 & 22 & 1 \\
& QuixBugs& [48] & 40 & 2 & 1 \\
\Block{2-1}{}\multirow{2}{*}{jMutRepair [27]} & Defects4J& & 224 & 17 & -- \\
& QuixBugs& [48] & 40 & 3 & 1 \\
Kali-A [50] & Defects4J& & 224 & 33 & -- \\
LSRepair [23] & Defects4J& & 395 & 38 & 19 \\
PAR [17] & \rlap{PARDataset} & & 119 & 27 & -- \\
\Block{2-1}{}\multirow{2}{*}{PSRepair-A [50]} & Defects4J& & 224 & 44 & -- \\
& QuixBugs& [48] & 40 & 4 & 2 \\
SimFix [14] & Defects4J& & 357 & 56 & 34 \\
\textsc{SketchFix} [13] & Defects4J& & 357 & 26 & 19 \\
SOFix [24] & Defects4J& & 224 & -- & 23 \\
ssFix [45] & Defects4J& & 357 & 60 & 20 \\
xPar [18] & Defects4J& & 90 & -- & 4 \\
\bottomrule
\end{NiceTabular}

\end{document}

3 BlackMild Aug 20 2020 at 04:28

แล้วอันนี้ละ? (ใช้matrixTikZ)

\documentclass[tikz,border=5mm]{standalone}
\usepackage{amsmath,amssymb,amsthm}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of nodes,draw=teal,thick,inner sep=.5pt,
nodes in empty cells,
row sep=-\pgflinewidth,
column sep=-\pgflinewidth,
nodes={minimum height=9mm,anchor=center},
row 1/.style={nodes={font=\bfseries}},
column 1/.style={nodes={minimum width=2.5cm,text width=1.5cm,align=left,font=\itshape}},
column 2/.style={nodes={minimum width=7cm,text width=6cm,align=left}},
every even row/.style={nodes={fill=cyan!20}}
]{
Variables & Descriptions\\  
ln(wage)                 & log of wage\\
educ                     & years of education\\
black                    & 1 if black and 0 if not\\
hisp                     & 1 if hispanic and 0 if not\\
exper                    & years of experience\\
exper\textsuperscript{2} & years of experience squared\\
married                  & 1 if married and 0 if not\\
union                    & 1 if belongs to a union and 0 if not\\
};
\end{tikzpicture}
\end{document}