LaTeX表格制作备忘一

  LaTeX的普通表格制作比较简单,但是,如果要精确控制格式,就有些麻烦了。今天在做一些文档,其中用到某一种表格,现把代码与说明记录于此以备忘。
  环境:Ubuntu 16.04 64位桌面版
  LaTeX编辑工具:TeXstudio
 
代码如下:

\documentclass[oneside, AutoFakeBold]{article}

\usepackage{geometry}          % 用于页面设置
% 设置为A4纸,并按照MSOffice的默认尺寸设置四周边距
\geometry{
  a4paper,
  left = 3.17cm,
  right = 3.17cm,
  top = 2.54cm,
  bottom = 2.54cm
}

\usepackage{xcolor}                   % 颜色支持
\definecolor{mygray}{gray}{0.9}       % 定义颜色
% 用于支持超链接,好像只有加载这个才能支持目录的跳转
\usepackage[colorlinks,linkcolor=blue!60!black]{hyperref}

\usepackage{makecell}                 % 用于支持表格单元格操作
\usepackage{colortbl}                 % 用于支持表格颜色
\usepackage{array}                    % 表格增强

\renewcommand{\theadgape}{\Gape[0.3cm][0.3cm]}  % 控制单元格内容的高度与深度

% ------------------ 开始 -------------------

\begin{document}

\begin{tabular}{|m{1cm}|m{13.1cm}|}
\hline
\rowcolor{mygray}   % 将此行背景色设置为灰色
\thead{\textbf{S.N.}} & \thead{\textbf{Memory Addresses \& Description}} \\
\hline
\thead{1} & \textbf{Symbolic addresses}\\
& \Gape[0cm][0cm]{\makecell[l]{The addresses used in a source code. The variable names, constants, and instruction\\ labels are the basic elements of the symbolic address space.}}\\
\hline
\thead{2} & \textbf{Relative addresses}\\
& \Gape[0cm][0cm]{\makecell[l]{At the time of compilation, a compiler converts symbolic addresses into relative\\ addresses.}}\\
\hline
\thead{3} & \textbf{Physical addresses}\\
& \Gape[0cm][0cm]{\makecell[l]{The loader generates these addresses at the time when a program is loaded into\\ main memory.}}\\
\hline
\end{tabular}

\end{document}

生成的表格如下:
LaTeX表格制作备忘一
 
说明:
 
1. 单元格内的强制换行
  通常情况下是不可以的,因为会被认为是表格的换行,这里用\makecell命令包含后就可以使用双斜杠来强制换行了。如果不强制换行,这里会只列出一行,文字内容会跑到外面去。虽然表格开头设置了具体的尺寸,但在\Gape命令的影响下,原本的自动换行无效。
 
2. 单元格文字与边框的距离
  这里是使用makecell宏包中的工具来实现的,我这里暂时只是控制上下的距离。设置这个就是为了更好看些。当然,这样的设置还是挺麻烦的,等以后找到更简便的方法后再修改。

猜你喜欢

转载自blog.51cto.com/14013986/2320622