[英]Convert tables in rst to LaTeX
我有一些 .rst 文件,我使用標准 sphinx 轉換器將它們轉換為 .tex 文件。
在某些 .rst 我有特殊寬度的表格,例如:
.. list-table::
:widths: 50 50
生成的 .tex 始終包含如下表:
\begin{tabulary}{\textwidth}{|L|L|}
因此,列寬丟失了。
將 rst 轉換為 Latex 時如何保留列寬?
我也用逗號分隔符,
.. list-table::
:widths: 50 , 50
:header-rows: 1
* - SETTING
- DESCRIPTION
* - Enable
- Enables or disables internal tracing.
* - Verbose
- Enables or disables extended internal tracing.
但它不起作用..也許我使用了一個壞的轉換器? 你推薦什么轉換器?
實際上是命令
.. tabularcolumns:: |p{4.5cm}|p{8.5cm}|
就在 .. list-table 之前需要:
https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-tabularcolumns
嘗試:
:widths: 50, 50
用逗號分隔符。
輸出還取決於您的表是如何寫入 rst 的。
我假設您使用的是標准的 rst 表格語法,而不是從項目符號列表中制作表格(盡可能)。 如需更多幫助,請嘗試http://docutils.sourceforge.net/docs/ref/rst/directives.html#tables
此外,如果50, 50
是列寬,則您的乳膠代碼應如下所示:
\begin{tabulary}{ 1\textwidth}{ | p{0.5} | p{0.5} | }
和:
\begin{tabulary}{total width of table}{| column width| column width|}
由於我有一個龐大的文檔,我試圖修復乳膠生成。 此外,我認為 rst 文件中的 Latex 符號是一個缺點,因為它不一致並且需要編輯器部分地學習一種敏感的標記語言。
我用我自己的版本替換了 LaTeXTranslator.depart_table。 我復制了原始的 depart_table 並添加了此代碼(已縮短):
def my_depart_table (self, node):
totalColwidth = 0
givenColwidth = []
hasColwidth = False
for tgroup in node:
for tableColspec in tgroup:
try:
if tableColspec.has_key('colwidth'):
totalColwidth += tableColspec['colwidth']
givenColwidth.append(tableColspec['colwidth'])
hasColwidth = True
except:
print "colspec missing. \n"
# original code
if hasColwidth:
colspec = ""
for thisColwidth in givenColwidth:
colspec += ('>{\RaggedRight}p{%.3f\\linewidth}' % (0.95 * thisColwidth / totalColwidth))
#print "using widths: %.3f %s %s" % ((0.95 * thisColwidth / totalColwidth), thisColwidth, totalColwidth)
self.body.append('{' + colspec + '}\n')
# more original code
LaTeXTranslator.depart_table = my_depart_table
我既不精通 Python 也不精通 Sphinx,因此使用風險自負。 我希望你能得到想法,甚至可以給出建議。
如果您使用 Python < 3.0 並希望完全刪除因子 0.95,請記住將其中一個整數轉換為浮點數或從 __ future __ 導入除法。
docutils rst2latex 作家有一些表格問題: http://docutils.sourceforge.net/docs/dev/todo.html#tables ,所以也許您的問題與此有關? 我認為 Sphinx 編寫器基於 rst2latex,因此可能存在相同的問題。
我可以確認這一點:
.. list-table::
:widths: 10 40 50
* - Module
- Link
- Description
與 rst2latex 一起使用
\setlength{\DUtablewidth}{\linewidth}
\begin{longtable*}[c]{|p{0.104\DUtablewidth}|p{0.375\DUtablewidth}|p{0.465\DUtablewidth}|}
\hline
Module
&
Link
&
Description
\\
\hline
但是有了獅身人面像,我明白了 OP 所說的。 所以不是我會收集的 rst2latex 問題。
文檔談論的“自動”寬度的東西對我來說也不是很有用,鏈接往往會流血。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.