如何在pylatex中的乳胶table的单元格中添加对图形的引用

How to add a reference to a figure in a cell of a latex table in pylatex

我正在使用 pylatex,我需要使用 "add_row" 命令在表格环境中的 latex table 单元格中添加对标记图形的引用。哪一个是正确的语法?似乎一个简单的字符串不起作用。

我尝试使用简单的字符串添加引用,即:

tabular.add_row('P' + str(j), param, 'Figure' + NoEscape('\ref{fig:param' + str(j) + '}'))

tabular.add_row('P' + str(j), param, 'Figure' + '\ref{fig:param' + str(j) + '}')

tabular.add_row('P' + str(j), param, 'Figure' + str(Ref('param' + str(j))))

以下是您可以在 Python 中 运行 的最小可重现示例。请将 HOME_FOLDER 设置为您的个人主文件夹(语法取决于 Windows 或 Linux,我使用的是 Linux)


from pylatex import Document, Package, Command, NoEscape, Section, Table, Center, Tabular, Figure, Label
import subprocess
import numpy as np
import matplotlib.pyplot as plt
import os

time = np.arange(0, 10, 0.001)
frequency = 0.2
y1 = np.sin(2*np.pi*frequency*time)
y2 = np.cos(2*np.pi*frequency*time)

HOME_FOLDER = os.path.join('/home', 'Documents')
texFileName = os.path.join(HOME_FOLDER, 'test')

plt.figure()
plt.plot(time, y1)
plt.savefig(os.path.join(HOME_FOLDER, 'firstParam.pdf'))
plt.close()

plt.figure()
plt.plot(time, y2)
plt.savefig(os.path.join(HOME_FOLDER, 'secondParam.pdf'))
plt.close()

geometry_options = {"top": "2.5cm", "left": "2.25cm", "right": "2.25cm", "bottom": "2.0cm"}

doc = Document(texFileName, documentclass="article", document_options=["12pt", "a4paper"], geometry_options=geometry_options)

doc.packages.append(Package('xcolor', 'table'))
doc.packages.append(Package("caption", "font=normalsize, tableposition=below"))
doc.preamble.append(Command('captionsetup', 'skip=1em', 'longtable'))
doc.packages.append(Package('fancyhdr'))
doc.preamble.append(Command('pagestyle', 'fancy'))
doc.preamble.append(Command('fancyhead', '', 'R'))
doc.packages.append(Package('titlesec'))
doc.preamble.append(Command('setcounter', 'secnumdepth', extra_arguments='4'))

doc.packages.append(Package(NoEscape('eso-pic')))
doc.packages.append(Package('adjustbox'))
doc.packages.append(Package('float'))
doc.packages.append(Package('pdfpages'))
doc.packages.append(Package('array'))
doc.packages.append(Package('hyperref', 'hidelinks'))

paramsList = ['firstParam', 'secondParam']

with doc.create(Section("First Section")):

    with doc.create(Table(position='ht')) as table:
        with table.create(Center()) as centered:
            with centered.create(Tabular('|l|c|c|')) as tabular:
                tabular.add_hline()
                for j, param in enumerate(paramsList):
                    tabular.add_row('P' + str(j), param, 'Figure' + '\ref{fig:param' + str(j) + '}')
                    tabular.add_hline()    
            table.add_caption('List of Parameters')

    for j, param in enumerate(paramsList):        
        with doc.create(Figure(position='H')) as plot:
            plot.add_image(os.path.join(HOME_FOLDER, param + '.pdf'))
            plot.add_caption(param)
            plot.append(Label('fig:param' + str(j)))

doc.generate_tex()
buildInfo = None

for _ in range(1):
        p = subprocess.Popen("pdflatex -synctex=1 -interaction=nonstopmode {} > LaTeX_Output.txt".format(texFileName), shell=True, bufsize=-1, 
                                stdout=subprocess.DEVNULL, cwd=None)
texFileName.replace('.tex', '.pdf')

我的预期结果是在 table 单元格中有一个可点击的 link 到相关参数图,即 Figure 1,而我看到的是纯文本,即 Figure er{fig:param0}

好的,我使用dumps_as_content()方法简单地解决了这个问题:

 with doc.create(Table(position='ht')) as table:
        with table.create(Center()) as centered:
            with centered.create(Tabular('|l|c|c|')) as tabular:
                tabular.add_hline()
                for j, param in enumerate(paramsList):
                    tabular.add_row('P' + str(j), param, NoEscape('Fig. ' + Ref('fig:param' + str(j)).dumps_as_content())))
                    tabular.add_hline()    
            table.add_caption('List of Parameters')