pylatex.base_classes.command

This module implements a class that implements a latex command.

This can be used directly or it can be inherited to make an easier interface to it.

class CommandBase(arguments=None, options=None, *, extra_arguments=None)[source]

Bases: pylatex.base_classes.latex_object.LatexObject

A class that represents a LaTeX command.

The name of this class (when lowercased) will be the name of this command. To supply a different name set the _latex_name attribute.

Parameters:
  • arguments (None, str, list or Arguments) – The main arguments of the command.
  • options (None, str, list or Options) – Options of the command. These are placed in front of the arguments.
  • extra_arguments (None, str, list or Arguments) – Extra arguments for the command. When these are supplied the options will be placed before them instead of before the normal arguments. This allows for a way of having one or more arguments before the options.
class Command(command=None, arguments=None, options=None, *, extra_arguments=None, packages=None)[source]

Bases: pylatex.base_classes.command.CommandBase

A class that represents a LaTeX command.

This class is meant for one-off commands. When a command of the same type is used multiple times it is better to subclass CommandBase.

Parameters:
  • command (str) – Name of the command
  • arguments (None, str, list or Arguments) – The main arguments of the command.
  • options (None, str, list or Options) – Options of the command. These are placed in front of the arguments.
  • extra_arguments (None, str, list or Arguments) – Extra arguments for the command. When these are supplied the options will be placed before them instead of before the normal arguments. This allows for a way of having one or more arguments before the options.
  • packages (list of Package instances) – A list of the packages that this command requires

Examples

>>> Command('documentclass',
>>>         options=Options('12pt', 'a4paper', 'twoside'),
>>>         arguments='article').dumps()
'\\documentclass[12pt,a4paper,twoside]{article}'
>>> Command('com')
'\\com'
>>> Command('com', 'first')
'\\com{first}'
>>> Command('com', 'first', 'option')
'\\com[option]{first}'
>>> Command('com', 'first', 'option', 'second')
'\\com{first}[option]{second}'
class UnsafeCommand(command=None, arguments=None, options=None, *, extra_arguments=None, packages=None)[source]

Bases: pylatex.base_classes.command.Command

An unsafe version of the Command class.

This class is meant for one-off commands that should not escape their arguments and options. Use this command with care and only use this when the arguments are hardcoded.

When an unsafe command of the same type is used multiple times it is better to subclass CommandBase and set the _default_escape attribute to false.

Parameters:
  • command (str) – Name of the command
  • arguments (None, str, list or Arguments) – The main arguments of the command.
  • options (None, str, list or Options) – Options of the command. These are placed in front of the arguments.
  • extra_arguments (None, str, list or Arguments) – Extra arguments for the command. When these are supplied the options will be placed before them instead of before the normal arguments. This allows for a way of having one or more arguments before the options.
  • packages (list of Package instances) – A list of the packages that this command requires

Examples

>>> Command('documentclass',
>>>         options=Options('12pt', 'a4paper', 'twoside'),
>>>         arguments='article').dumps()
'\\documentclass[12pt,a4paper,twoside]{article}'
>>> Command('com')
'\\com'
>>> Command('com', 'first')
'\\com{first}'
>>> Command('com', 'first', 'option')
'\\com[option]{first}'
>>> Command('com', 'first', 'option', 'second')
'\\com{first}[option]{second}'
class Parameters(*args, **kwargs)[source]

Bases: pylatex.base_classes.latex_object.LatexObject

The base class used by Options and Arguments.

This class should probably never be used on its own and inhereting from it is only useful if a class like Options or Arguments is needed again.

Parameters:
  • *args – Positional parameters
  • **kwargs – Keyword parameters
class Options(*args, **kwargs)[source]

Bases: pylatex.base_classes.command.Parameters

A class implementing LaTex options for a command.

It supports normal positional parameters, as well as key-value pairs. Options are the part of a command located between the square brackets ([]). The positional parameters will be outputted in order and will appear before the key-value-pairs. The key value-pairs won’t be outputted in the order in which they were entered

Examples

>>> args = Options('a', 'b', 'c').dumps()
'[a,b,c]'
>>> Options('clip', width=50, height='25em', trim='1 2 3 4').dumps()
'[clip,trim=1 2 3 4,width=50,height=25em]'
Parameters:
  • *args – Positional parameters
  • **kwargs – Keyword parameters
class SpecialOptions(*args, **kwargs)[source]

Bases: pylatex.base_classes.command.Options

A class that sepparates the options with ‘][‘ instead of ‘,’.

Parameters:
  • *args – Positional parameters
  • **kwargs – Keyword parameters
class Arguments(*args, **kwargs)[source]

Bases: pylatex.base_classes.command.Parameters

A class implementing LaTex arguments for a command.

It supports normal positional parameters, as well as key-value pairs. Arguments are the part of a command located between the curly braces ({}). The positional parameters will be outputted in order and will appear before the key-value-pairs. The key value-pairs won’t be outputted in the order in which they were entered

Examples

>>> args = Arguments('a', 'b', 'c').dumps()
'{a}{b}{c}'
>>> args = Arguments('clip', width=50, height='25em').dumps()
>>> args.dumps()
'{clip}{width=50}{height=25em}'
Parameters:
  • *args – Positional parameters
  • **kwargs – Keyword parameters