Latex技巧--表格相关操作

wan123 1天前 阅读数 4756 #软件测试

表格同时合并行合并列与斜杠

Latex技术

  • 文档导言区:multirow、diagbox
    • 环境:{table}[htbp]
      • 命令:\centering
      • 命令:\caption{context}
    • 环境:{tabular}{|*{3}{c|}}
      • 命令:\multicolumn{1}{|p{5cm}|}{context}
      • 命令:\multirow{2}{0.2\textwidth}{context}
      • 命令:\diagbox[dir=NW]{context}{context}
      • 命令:\makecell[c]{line1\line2}

表格同时合并行合并列与斜杠

表格合并列

Latex默认只支持合并列,使用了multicolumn命令会屏蔽tabular导言中对该列的定义。

\multicolumn{3}{|c|}{建设期}

表格同时合并行合并列

先使用命令multicolumn,再嵌套使用\multirow。如果字体溢出表格,需要通过0.5\textwidth与p{5cm}的方式进行调整。

\multicolumn{2}{|c|}{\multirow{2}{*}{合并两行两列}}

表格同时合并行合并列与斜杠

在multcolumn嵌套multirow,再嵌套diagbox后,还需要调整diagbox参数,因为默认diagbox的宽度与tabular导言单位一致,需要调整width参数。

\multicolumn{2}{c}{\multirow{2}{*}{\diagbox[width=5em]{项目}{时间}}}

Demo

一个完整的示例。

\begin{table}[htbp]
\centering
\caption{三种方案的现金流}
\begin{tabular}{|*{10}{c|}}
\hline
\multicolumn{2}{|p{5cm}<{\centering}|}{\multirow{2}{*}{\diagbox[width=14.5em]{方案}{阶段}}}&
\multicolumn{3}{p{2cm}<{\centering}|}{建设期}&
\multicolumn{5}{p{7cm}<{\centering}|}{运营期}\\
\cline{3-10}
\multicolumn{2}{|c|}{~}&0&1&合计&2&3&4&5&合计\\
\hline
\multirow{2}{*}{方案一}&年初投资额&350&150&500&&&&&\\
\cline{2-10}
&年末净现金流量&&&&150&200&250&400&1000\\
\hline
\end{tabular}
\end{table}

效果:

在表格的单元格中插入图片

Latex技术

  • 文档导言区:graphicx,makecell
    • 环境:{minipage}[b]{0.5\linewidth}
      • 命令:\raisebox{-0.5/height}{context}
      • 命令:\includegraphics[width=0.5\textwidth]{context}
      • 命令:\makecell[c]{lien1\line2}
    • 参数:
      • \textwidth、\hsize:一行文字的宽度
      • \linewidth:当前行的宽度
      • \columnwidth:每一栏的宽度

在表格的单元格中插入图片方法

  • 在tabular环境中,建立待插入图片的单元格
  • 在相应单元格中嵌套minipage环境与raisebox、includegraphics、makecell等命令

Demo

\begin{table}[htbp]
\centering
\caption{minipage includegraphics}
\begin{tabular}{|*{2}{c|}}
\hline
说明&图片\\
\hline
\makecell[l]{solongcontextsolongcontextsolongcontextsolongc\\ontextsolongcontextsolongcontextsolongco\\ntextsolongcontextsolongcontextsolongcontext}&
\begin{minipage}[b]{.5\linewidth}
\centering
\raisebox{-0.5\height}{\includegraphics[width=.8\linewidth]{showimage.png}}
\end{minipage}
\\
\hline
\end{tabular}
\end{table}

效果:

将Excel文件导入到Latex

Latex技术

  • 文档导言区:booktabs
    • 环境:{table}
      • 命令:\belowrulesep=0pt %竖线间断问题
      • 命令:\aboverulesep=0pt %竖线间断问题
    • 环境:{tabular}{|*{3}{c|}}
      • 命令:\multicolumn{1}{|p{5cm}|}{context}
      • 命令:\multirow{2}{0.2\textwidth}{context}
      • 命令:\toprule
      • 命令:\midrule
      • 命令:\bottomrule
      • 命令:\cmidrule{1-2}
      • 命令:\addlinespace[1ex]

Excel文件导入到Latex方法

  • 在Excel中安装加载项Excel2LaTeX
  • 插件得到的latex代码直接复制使用,并微调效果

Demo

  • 下载Excel2LaTeX插件
  • Excel中:文件–>选项–>信任中心设置–>宏设置–>启动所有宏
  • Excel中:文件–>选项–>加载项–>管理Excel加载项–>加载Excel2LaTeX.xla文件
  • Excel中:加载项–>Convert Table to LaTeX
    复制代码示例:
% Table generated by Excel2LaTeX from sheet 'Sheet2'
\begin{table}[htbp]
  \centering
  \belowrulesep=0pt
  \aboverulesep=0pt
  \caption{Add caption}
    \begin{tabular}{|r|l|r|r|}
    \toprule
    \multicolumn{1}{|r|}{\multirow{2}[4]{*}{\textbf{设备}}} & \multirow{2}[4]{*}{\textbf{规格}} & \multicolumn{2}{l|}{\textbf{数量}} \\
\cmidrule{3-4}          &       & \multicolumn{1}{p{4.125em}|}{\textbf{区域1}} & \multicolumn{1}{p{4.125em}|}{\textbf{区域2}} \\
    \midrule
    \multicolumn{1}{|r|}{\multirow{2}[4]{*}{设备1}} & 规格1   & 5     & 5 \\
\cmidrule{2-4}          & 规格2   & 1     & 4 \\
    \bottomrule
    \end{tabular}%
  \label{tab:addlabel}%
\end{table}%

结束语

读书是学习,使用也是学习,而且是更重要的学习。软件工程师在项目中实际使用Latex技术带来很多益处:履行文档驱动的开发方法,方便通过git实现可追溯性。

  • 随机文章
  • 热门文章
  • 热评文章
热门