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

时间:2019-06-04 03:39:57

标签: python label latex ref pylatex

我正在使用pylatex,我需要使用“ add_row”命令在表格环境中的乳胶表单元格中添加对带标签图形的引用。正确的语法是哪一种?看来一个简单的字符串不起作用。

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

if ($value = $this->getValue('key')) { // Runs if value is truthy }

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) + '}')

在此之后,您可以在Python中运行一个最小的可复制示例。请为您的个人主文件夹设置tabular.add_row('P' + str(j), param, 'Figure' + str(Ref('param' + str(j))))(语法取决于Windows或Linux,我使用的是Linux)

HOME_FOLDER

我的预期结果是在表格单元格中具有指向相关参数图即 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') 的可单击链接,而我看到的是纯文本即Figure 1

1 个答案:

答案 0 :(得分:0)

好的,我只需使用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')