pylatex Command

1,395

The .dumps() function is useful for debugging, because it allows you to see generated commands in the Python console. However, to include commands in the LaTeX document you should use .append with the command itself as argument (and not the dump).

MWE:

import pylatex as pyl

doc = pyl.Document()
doc.packages.append(pyl.Package('graphicx'))
doc.packages.append(pyl.Package('fancyhdr'))

doc.preamble.append(pyl.Command('pagestyle', 'fancy'))
doc.preamble.append(pyl.Command('rhead',arguments=pyl.NoEscape(r'\begin{picture}(0,0) \put(0,0){\includegraphics[width=4cm]{example-image}} \end{picture}')))

doc.append('abc')

doc.generate_pdf('rheadtest', clean_tex=False)

Result (.tex):

\documentclass{article}%
\usepackage[T1]{fontenc}%
\usepackage[utf8]{inputenc}%
\usepackage{lmodern}%
\usepackage{textcomp}%
\usepackage{lastpage}%
\usepackage{graphicx}%
\usepackage{fancyhdr}%
%
\pagestyle{fancy}%
\rhead{\begin{picture}(0,0) \put(0,0){\includegraphics[width=4cm]{example-image}} \end{picture}}%
%
\begin{document}%
\normalsize%
abc%
\end{document}

Result (.pdf):

enter image description here

Share:
1,395

Related videos on Youtube

scana
Author by

scana

Updated on December 07, 2020

Comments

  • scana
    scana almost 3 years

    I'm using pylatex and I need to attach a logo in the header.

    I want to use this latex example:

    \documentclass{article}
    \usepackage[tmargin=3cm,lmargin=3cm,rmargin=2cm]{geometry}
    \usepackage{geometry}
    \usepackage{graphicx}
    \usepackage{fancyhdr}
    \usepackage{xcolor}
    \pagestyle{fancy}
    \renewcommand{\headrulewidth}{0pt}
    \rhead{\begin{picture}(0,0) \put(-500,0){\includegraphics[width=5cm]{logo}} \end{picture}}
    \begin{document}
    SOMETHING
    \end{document}
    

    I tried with this:

    import pylatex as pyl
        pyl.Command('rhead',
                arguments=NoEscape(r'\begin{picture}(0,0) \put(0,0){\includegraphics[width=4cm]{logo}} \end{picture}')).dumps()
    

    But is does not work. I do not get an error, it simly does not include it in the tex file. Thank you

    • Alessandro Cuttin
      Alessandro Cuttin almost 5 years
      Please add a minimal working example (MWE) that illustrates your problem.
    • scana
      scana almost 5 years
      I edited the question
  • scana
    scana almost 5 years
    doc.preamble!!! I've been looking for something like this for long. Thank you!