[英]Change Latex Table Style using Sphinx
I'm using sphinx
to generate our company manuals. 我正在使用
sphinx
生成我们的公司手册。 In our legacy manuals we have a certain table style, that we would like to keep. 在旧版手册中,我们希望保留某种表格样式。
The table style consists of tables having a header with dark green background, then there are several light-green background rows and after that several rows with a white background. 表格样式由具有标题为深绿色背景的表格组成,然后有几行浅绿色的背景行,然后是几行具有白色背景的行。
Sphinx
generates my LaTeX input files, which are then made an PDF using PdfLatex. Sphinx
生成我的LaTeX输入文件,然后使用PdfLatex将其制成PDF。 There is the possibility to include LaTeX specific comments right before the LaTeX table starts with :raw-role:
. 在LaTeX表以
:raw-role:
开头之前,可以包含LaTeX特定注释。
The input RST file: 输入RST文件:
.. role:: raw-role(raw)
:format: latex
:raw-role:`\firstRow`
=== === ===
A B C
=== === ===
1A 1B 1C
2A 2B 2C
3A 3B 3C
4A 4B 4C
=== === ===
Will be transformed into: 将转换为:
\firstRow
\noindent\begin{tabulary}{\linewidth}{|L|L|L|}
\hline
\sphinxstylethead{\relax
A
\unskip}\relax &\sphinxstylethead{\relax
B
\unskip}\relax &\sphinxstylethead{\relax
C
\unskip}\relax \\
\hline
1A
&
1B
&
1C
\\
\hline
2A
&
2B
&
2C
\\
\hline
3A
&
3B
&
3C
\\
\hline
4A
&
4B
&
4C
\\
\hline\end{tabulary}
I'm now in need for a command called \\firstRow
that modifies the behaviour of the \\tabulary
environment, so that the header has a dark-green background, the first row beneath has a light-green background and the remaining rows having a white background. 现在,我需要一个名为
\\firstRow
的命令,该命令可以修改\\tabulary
环境的行为,以便标头具有深绿色的背景,下面的第一行具有浅绿色的背景,其余行具有白色背景。
If everything else fails there might be still the possibility to automatically replace the contents of the cells in the RST-File using a tool like pandoc
. 如果其他所有操作均失败,则仍有可能使用
pandoc
类的工具自动替换RST-File中的单元格内容。 For example it might be possible to replace 1A
by :raw-role:'\\background{green}' 1A
in the RST file and then to transform it with sphinx
. 例如,可以在RST文件中用
:raw-role:'\\background{green}' 1A
替换1A
,然后用sphinx
对其进行转换。
I found it interesting that in case of the sphinx HTML
target the problem can be very easily solved by using an adapted style.css
containing the class firstRow
together with a command like .. rst-class:: firstRow
, where firstRow
represents a table style with one light green background. 我发现有趣的是,在使用狮身人面像
HTML
目标的情况下,可以通过使用改编后的style.css
轻松解决问题,该类包含firstRow
类以及类似.. rst-class:: firstRow
的命令,其中firstRow
代表表格样式有一个浅绿色的背景。
Unfortunately, the LaTeX target misses something similar to style.css
. 不幸的是,LaTeX目标缺少与
style.css
类似的东西。
Generally speaking, LaTeX is in no way as customizable as HTML+CSS. 一般来说,LaTeX绝不像HTML + CSS那样可定制。
There might be some package providing convenient interface to colorizing the first two rows of a table, unfortunately I am not aware of one which would work with a macro like \\firstRow
before the table. 可能有一些程序包提供了方便的接口来为表的前两行着色,不幸的是,我不知道有哪个程序包可以在表之前使用
\\firstRow
类的宏。 The xcolor
package provides (if loaded with option table) a \\rowcolors
command, but it does not seem to suffice. xcolor
包提供了\\rowcolors
命令(如果与选项表一起加载),但是似乎不够用。 It is easy to obtain the desired goal by manual mark-up of the LaTeX source, but it is more complicated achieving this solely using \\firstRow
macro. 通过手动标记LaTeX源很容易获得期望的目标,但是仅使用
\\firstRow
宏来实现此目标会更加复杂。
I ended up with a convoluted approach, which I tested on Sphinx 1.5.3. 我最终采用了一种复杂的方法,并在Sphinx 1.5.3上进行了测试。
in conf.py
: 在
conf.py
:
latex_elements = {
'preamble': r"""
\usepackage{colortbl}
\protected\def\sphinxstylethead {\cellcolor{green}\textsf}
"""
}
Edit: since Sphinx 1.6, \\sphinxstylethead
is deprecated and \\sphinxstyletheadfamily
should be used instead . 编辑:既然狮身人面像1.6
\\sphinxstylethead
已被废弃, \\sphinxstyletheadfamily
应改为使用 。 This means that the above should now be: 这意味着上述内容应为:
latex_elements = {
'preamble': r"""
\usepackage{colortbl}
\protected\def\sphinxstyletheadfamily {\cellcolor{green}\sffamily}
"""
}
The former version works with Sphinx 1.6 but will fail with 1.7 when \\sphinxstylethead
will stopped being used by Sphinx. 以前的版本适用于Sphinx 1.6,但是当
\\sphinxstylethead
将不再被Sphinx使用时,它将无法使用1.7。
in reST sources, something like this: 在reST来源中,如下所示:
====== === ===
A B C
====== === ===
|x| 1A 1B 1C
2A 2B 2C
3A 3B 3C
4A 4B 4C
====== === ===
.. |x| raw:: latex
\rowcolor{blue}
Of course change colours to your liking. 当然可以根据您的喜好更改颜色。 You may need to pass an option like
dvipsnames
to xcolor
package, and then you can use things like \\rowcolor[named]{ForestGreen}
. 您可能需要将
dvipsnames
类的选项dvipsnames
给xcolor
包,然后才能使用\\rowcolor[named]{ForestGreen}
。
Sphinx 1.6 will have table templates allowing some easier customization but still this will be a looooong way from what one can do in HTML+CSS. Sphinx 1.6将具有表模板,允许进行一些更轻松的自定义,但这仍然是与HTML + CSS相比可以轻松实现的一种方式。
For completeness here is how to pass the dvipsnames
option to xcolor
: 为了完整
dvipsnames
这里是如何将dvipsnames
选项传递给xcolor
:
latex_elements = {
'passoptionstopackages': '\\PassOptionsToPackage{dvipsnames}{xcolor}',
'preamble': r"""
\usepackage{colortbl}
% for Sphinx 1.5.x (1.6 ok, but 1.7 not):
\protected\def\sphinxstylethead {\cellcolor{Aquamarine}\textsf}
% better to use rather this with Sphinx 1.6 and mandatory if Sphinx 1.7:
% \protected\def\sphinxstyletheadfamily {\cellcolor{Aquamarine}\sffamily}
""",
}
Also, the [named]
I mentioned above ( \\rowcolor[named]{ForestGreen}
) is not needed with xcolor
and I have not used it in 'preamble'
config here. 此外,
[named]
我上面提到的( \\rowcolor[named]{ForestGreen}
)不需要与xcolor
,我还没有在使用它'preamble'
配置在这里。
It is needed when only color
package is used, but Sphinx uses xcolor
if available. 当只在需要
color
是用来包,但狮身人面像使用xcolor
(如果可用)。
Loading colortbl
is not needed if one also passes option table
to xcolor
, and is perhaps better way in case the need for use of xcolor
's \\rowcolors
emerges at a later stage. 如果也将选项
table
也传递给xcolor
,则不需要加载colortbl
,这可能是更好的方法,以防在以后出现使用xcolor
的\\rowcolors
。
Merged cells from grid table cause arduous issues and Sphinx 1.6 will be much better than currently in this respect, however in current state of development, merged cells (multirow, multicolumn, or both) are set to simply ignore table colour commands. 网格表中的合并单元格引起了艰巨的问题,Sphinx 1.6在这方面要比当前好得多,但是在当前的开发状态下,合并单元格(多行,多列或两者)都设置为仅忽略表颜色命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.