Skip to content

about command and math #390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions pylatex/base_classes/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,41 @@ def __init__(
if packages is not None:
self.packages |= packages

def __call__(*args, **kwargs):
"""Make the command callable

Examples
--------
>>> Command('com')('first') # === Command('com', 'first')
>>> com = Command('com') # or com = Command('com', 'first')
>>> com('one') # === Command('com', 'one')
>>> com('another') # === Command('com', 'another')

Returns
-------
Command
"""

return Command(self.latex_name, *args, **kwargs)


class slash:

r"""
Imitate the slash symbol `\` in LaTeX

>>> __ = slash()
>>> print(__.item.dumps())
'\item'
>>> print(__.frac(('a', 'b')).dumps())
'\frac{a}{b}'
"""

def __getattr__(self, name):
return Command(name)

__ = slash()


class UnsafeCommand(Command):
"""An unsafe version of the `Command` class.
Expand Down
77 changes: 74 additions & 3 deletions pylatex/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,23 @@ class Math(Container):

content_separator = " "

def __init__(self, *, inline=False, data=None, escape=None):
def __init__(self, *, inline=False, data=None, escape=None, dollar=False):
r"""
Args
----
data: list
Content of the math container.
inline: bool
If the math should be displayed inline or not.
escape : bool
escape: bool
if True, will escape strings
dollar: bool
if True, use $$<math formula>$$ instead of \[<math formula>\]
"""

self.inline = inline
self.escape = escape
self.dollar = dollar
super().__init__(data=data)

def dumps(self):
Expand All @@ -70,7 +73,10 @@ def dumps(self):
"""
if self.inline:
return "$" + self.dumps_content() + "$"
return "\\[%\n" + self.dumps_content() + "%\n\\]"
if self.dollar:
return '$$\n' + self.dumps_content() + '\n$$'
else:
return '\\[%\n' + self.dumps_content() + '%\n\\]'


class VectorName(Command):
Expand Down Expand Up @@ -154,3 +160,68 @@ def dumps_content(self):
super().dumps_content()

return string


class Determinant(Matrix):
"""A sublcass of `Matrix`, representing the determinant of a matrix."""

def __init__(self, matrix, *args, **kwargs):
"""
Args
---
matrix: numpy.ndarray
The square matrix
"""
super().__init__(matrix, mtype='v', *args, **kwargs)
assert self.matrix.ndim == 2 and \
self.matrix.shape[1] == self.matrix.shape[0]


class Vector(Matrix):
"""
A subclass of `Matrix`, representing a vector. If it receives a matrix,
then the matrix will be reshaped.
"""

def __init__(self, vec, *args, **kwargs):
r"""
Args
----
vec: `numpy.ndarray` instance
"""
super().__init__(matrix=vec, *args, **kwargs)
size = self.matrix.size
self.matrix = self.matrix.reshape(1, size)


class ColumnVector(Vector):
"""a subclass of `Vector`, representing a column vector."""

def __init__(self, *args, **kwargs):
"""As the transposition of `Vector`"""
super().__init__(*args, **kwargs)
self.matrix = self.matrix.T


def dollar(x, *args, **kwargs):
"""Shorthand for inline math form: $math expression$.

Example
---
>>> dollar('c_B')
$c_B$
"""
return Math(data=x, inline=True, *args, **kwargs)


def ddollar(x, *args, **kwargs):
"""Shorthand for math form: $$math expression$$, or $$math expression$$.

Example
---
>>> ddollar('c_B')
$$
c_B
$$
"""
return Math(data=x, dollar=True, *args, **kwargs)