I'm trying to make the LaTeX command below utilizing the pyLaTeX library in a hopes to call this function later in the document.
\newcommand*\Entry[2]{%
\sffamily#1 & #2}
I'm attempting to follow along with the "Own Commands Example" from the official pyLaTeX docs but have some questions. The relevant parts of the doc follow below:
class ExampleCommand(CommandBase):
"""
A class representing a custom LaTeX command.
This class represents a custom LaTeX command named
``exampleCommand``.
"""
_latex_name = 'exampleCommand'
packages = [Package('color')]
doc = Document()
with doc.create(Section('Custom commands')):
# Define the new command
new_comm = UnsafeCommand('newcommand', '\exampleCommand', options=3,
extra_arguments=r'\color{#1} #2 #3 \color{black}')
doc.append(new_comm)
# Use our newly created command with different arguments
doc.append(ExampleCommand(arguments=Arguments('blue', 'Hello', 'World!')))
doc.append(ExampleCommand(arguments=Arguments('green', 'Hello', 'World!')))
doc.append(ExampleCommand(arguments=Arguments('red', 'Hello', 'World!')))
Which yeilds the LaTeX:
\newcommand{\exampleCommand}[3]{\color{#1} #2 #3 \color{black}}%
\exampleCommand{blue}{Hello}{World!}%
\exampleCommand{green}{Hello}{World!}%
\exampleCommand{red}{Hello}{World!}
My question is what is the purpose of the Class in this example? Is it only required because we are importing a LaTeX package? Would such a class be required for the command I am trying to create?
Furthermore, if the class is establishing the command than what is the purpose of the new_comm function? It isn't called at any other time in the code example so I am unsure of its purpose.
Thanks
Best Answer
You don't necessarily need to use the functions and classes that PyLaTeX provides. You can add any arbitrary text or LaTeX code to your document as strings using the
NoEscape
wrapper. To add a package there isdoc.packages.append
, for other preamble code such as command definitions (which are generally also in the preamble) there isdoc.preamble.append
, for the document itselfdoc.append
can be used.MWE for both the manual example and your own command:
Generated
pycommands.tex
:Result:
However, PyLaTeX is not intended to be just a wrapper around regular LaTeX code. The idea is that many relevant parts of LaTeX syntax are formalized, so LaTeX commands can be easily called with arguments stored in Python data structures, and that the document itself can be generated in a flexible way from Python code.
For this intended use you do need the classes and commands. The
CommandBase
class from the manual provides not only a way to indicate that this command needs thecolor
package, but it also generates the interface for the command that allows to provide the arguments as a list of Python strings or variables, i.e., theExampleCommand(arguments=Arguments('blue', 'Hello', 'World!'))
syntax.The
new_comm
part results in adding the\newcommand
code to the document, this is a separate part of the code because it is only needed for custom commands. The class definition however is needed for all commands if you want to use the syntax in the manual (except for commands implemented by PyLaTeX itself, such asHugeText
orTextColor
).Alternatively, you can use the generic
Command
class and provide the LaTeX name as first argument. In that case you don't need a separate class definition. Example: