From 4e4d7e3d95d9f3076616797dbca259d7abacb754 Mon Sep 17 00:00:00 2001 From: William Song <30965609+Freakwill@users.noreply.github.com> Date: Fri, 21 Mar 2025 14:20:32 +0800 Subject: [PATCH] cmd --- pylatex/base_classes/command.py | 35 +++++++++++++++ pylatex/math.py | 77 +++++++++++++++++++++++++++++++-- 2 files changed, 109 insertions(+), 3 deletions(-) diff --git a/pylatex/base_classes/command.py b/pylatex/base_classes/command.py index 071be847..4fb472b5 100644 --- a/pylatex/base_classes/command.py +++ b/pylatex/base_classes/command.py @@ -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. diff --git a/pylatex/math.py b/pylatex/math.py index 19a1b9b2..dbfc4830 100644 --- a/pylatex/math.py +++ b/pylatex/math.py @@ -44,7 +44,7 @@ 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 ---- @@ -52,12 +52,15 @@ def __init__(self, *, inline=False, data=None, escape=None): 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 $$$$ instead of \[\] """ self.inline = inline self.escape = escape + self.dollar = dollar super().__init__(data=data) def dumps(self): @@ -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): @@ -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)