0

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

1

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

  • doc.preamble!!! I've been looking for something like this for long. Thank you! – scana Dec 6 '18 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.