使用 pylatex 在图形中绘制任何东西

使用 pylatex 在图形中绘制任何东西

我想知道如何在图中画一些线条或者在图像上添加一些线宽,但我尝试过但没有得到任何解决方案。

这是我正在尝试的代码:

from pylatex import Document, PageStyle, Head, MiniPage, Foot, LargeText, \
MediumText, LineBreak, simple_page_number, Figure, NoEscape,\
StandAloneGraphic, Package, Command, VerticalSpace,\
HugeText, Section, NewPage, Center, TikZDraw,\
TikZCoordinate, TikZOptions, TikZ, TikZNode
from pylatex.utils import bold 
import os


def generate_header():
    #Imagenes Portada
    image = os.path.join(os.path.dirname(__file__), 'logo.png')

    with doc.create(Center()) as center:
    center.append(VerticalSpace("20pt"))
         with center.create(Section('Screen 1', numbering=True)):
              center.append(VerticalSpace("20pt"))
              with center.create(Figure(position='h!')) as image1:
                  image1.add_image(image, width='317px')
                  image1.add_caption('Description')

                  with image1.create(TikZ()) as pic:
                      # pic.append(TikZDraw([TikZCoordinate(-5, 0),
                      #             'rectangle',
                      #             TikZCoordinate(7, -2)],
                      #             ))
                       pic.append(TikZDraw([TikZCoordinate(0,0),
                            'circle'],
                            options=TikZOptions(radius='5pt')
                            ))

我得到的是图像下方的圆圈,而不是图形上方的圆圈。

答案1

这可能是糟糕的代码,但我想它是可行的。我没有使用与评论中链接的帖子完全相同的策略,因为我太愚蠢/懒惰,无法弄清楚如何做到这一点。具体来说,我删除了scope,这意味着坐标与图像大小无关。因此,如果您更改图像的大小,则必须更改所有路径的坐标。设置的方式是,图像的左下角位于坐标 (0,0)。

在此处输入图片描述

from pylatex import Document, PageStyle, Head, MiniPage, Foot, LargeText, \
MediumText, LineBreak, simple_page_number, Figure, NoEscape,\
StandAloneGraphic, Package, Command, VerticalSpace,\
HugeText, Section, NewPage, Center, TikZDraw,\
TikZCoordinate, TikZOptions, TikZ, TikZNode
from pylatex.utils import bold 
import os


def generate_header():
    #Imagenes Portada
    image = 'example-image'

    with doc.create(Center()) as center:
        center.append(VerticalSpace("20pt"))
        with center.create(Section('Screen 1', numbering=True)):
            center.append(VerticalSpace("20pt"))
            with center.create(Figure(position='h!')) as image1:
                with image1.create(TikZ()) as pic:
                    pic.append(TikZNode(text=StandAloneGraphic(image,image_options='width=7cm').dumps(),
                                options=TikZOptions('inner sep=0pt,name=img', 'anchor=south west')
                                ))
                    pic.append(TikZDraw([TikZCoordinate(0.1, 3), '--', TikZCoordinate(6, 4)], options=TikZOptions('draw', 'red', 'ultra thick', '-stealth')))
                    pic.append(TikZDraw([TikZCoordinate(3.5,0.5), 'rectangle', TikZCoordinate(6, 2)],
                                         options=TikZOptions('draw','blue','thick')))
                
doc = Document()
                
generate_header()

doc.generate_tex('my_solution')

生成以下代码:

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{ragged2e}%
\usepackage{tikz}%
%
%
%
\begin{document}%
\normalsize%
\begin{center}%
\vspace*{20pt}%
\section{Screen 1}%
\label{sec:Screen1}%
\vspace*{20pt}%


\begin{figure}[h!]%
\begin{tikzpicture}%
\node[inner sep=0pt,name=img,anchor=south west] {\includegraphics[width=7cm]{example-image}};%
\path[draw,red,ultra thick,-stealth,draw] (0.1,3.0) -- (6.0,4.0);%
\path[draw,blue,thick,draw] (3.5,0.5) rectangle (6.0,2.0);%
\end{tikzpicture}%
\end{figure}

%
\end{center}%
\end{document}

相关内容