Skip to content
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

Unit name for quantities only for a single character #347

Open
wants to merge 6 commits 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
2 changes: 1 addition & 1 deletion pylatex/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def generate_pdf(self, filepath=None, *, clean=True, clean_tex=True,

else:
# Notify user that none of the compilers worked.
raise(CompilerError(
raise (CompilerError(
'No LaTex compiler was found\n'
'Either specify a LaTex compiler '
'or make sure you have latexmk or pdfLaTex installed.'
Expand Down
1 change: 1 addition & 0 deletions pylatex/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ def __init__(self, name, options=None):
"""

super().__init__(arguments=name, options=options)

12 changes: 11 additions & 1 deletion pylatex/quantities.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
'Celsius': 'celsius',
'revolutions_per_minute': 'rpm',
'v': 'volt',
'arcdegree': 'degree'
}


Expand All @@ -46,7 +47,7 @@ def _dimensionality_to_siunitx(dim):
# Split unitname into prefix and actual name if possible
if unit.name.startswith(prefix):
substring += '\\' + prefix
name = unit.name[len(prefix)]
name = unit.name[len(prefix):]
break
else:
# Otherwise simply use the full name
Expand Down Expand Up @@ -141,3 +142,12 @@ def _format(val):
self.arguments._escape = False # dash in e.g. \num{3 +- 2}
if self.options is not None:
self.options._escape = False # siunitx uses dashes in kwargs

def __str__(self):
return self.dumps()

def __add__(self, other):
return str(self) + other

def __radd__(self, other):
return other + str(self)
27 changes: 20 additions & 7 deletions pylatex/tikz.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import math


class TikZLibrary(Package):
_latex_name = "usetikzlibrary"


class TikZOptions(Options):
"""Options class, do not escape."""

Expand All @@ -27,13 +31,15 @@ class TikZ(Environment):
"""Basic TikZ container class."""

_latex_name = 'tikzpicture'
packages = [Package('tikz')]
packages = [Package('tikz'), TikZLibrary('positioning'),Package('pgfplots'), Command('pgfplotsset', 'compat=newest')]


class Axis(Environment):
"""PGFPlots axis container class, this contains plots."""

packages = [Package('pgfplots'), Command('pgfplotsset', 'compat=newest')]
packages = [Package('pgfplots'),
Command('pgfplotsset', 'compat=newest'),
Command('usepgfplotslibrary','groupplots')]

def __init__(self, options=None, *, data=None):
"""
Expand Down Expand Up @@ -200,8 +206,9 @@ class TikZNode(TikZObject):
"""A class that represents a TiKZ node."""

_possible_anchors = ['north', 'south', 'east', 'west']
_counter = 0

def __init__(self, handle=None, options=None, at=None, text=None):
def __init__(self, handle=None, options=None, at=None, text=None, positioning=None):
"""
Args
----
Expand All @@ -215,9 +222,13 @@ def __init__(self, handle=None, options=None, at=None, text=None):
Body text of the node
"""
super(TikZNode, self).__init__(options=options)

if not handle:
handle = f'Node{TikZNode._counter}'
TikZNode._counter += 1
self.handle = handle

self._positioning = positioning

if isinstance(at, (TikZCoordinate, type(None))):
self._node_position = at
else:
Expand All @@ -230,15 +241,17 @@ def __init__(self, handle=None, options=None, at=None, text=None):
def dumps(self):
"""Return string representation of the node."""

ret_str = []
ret_str.append(Command('node', options=self.options).dumps())
ret_str = [Command('node', options=self.options).dumps()]

if self.handle is not None:
ret_str.append('({})'.format(self.handle))

if self._node_position is not None:
ret_str.append('at {}'.format(str(self._node_position)))

if self._positioning is not None:
ret_str.append(f'[{self._positioning[1]} = of {self._positioning[0].handle}]')

if self._node_text is not None:
ret_str.append('{{{text}}};'.format(text=self._node_text))
else:
Expand Down Expand Up @@ -550,7 +563,7 @@ def dumps(self):
self.error_bar):
# ie: "(x,y) +- (e_x,e_y)"
string += '(' + str(x) + ',' + str(y) + \
') +- (' + str(e_x) + ',' + str(e_y) + ')%\n'
') +- (' + str(e_x) + ',' + str(e_y) + ')%\n'

string += '};%\n%\n'

Expand Down
48 changes: 24 additions & 24 deletions pylatex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,6 @@ def _is_iterable(element):
return hasattr(element, '__iter__') and not isinstance(element, str)


class NoEscape(str):
"""
A simple string class that is not escaped.

When a `.NoEscape` string is added to another `.NoEscape` string it will
produce a `.NoEscape` string. If it is added to normal string it will
produce a normal string.

Args
----
string: str
The content of the `NoEscape` string.
"""

def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self)

def __add__(self, right):
s = super().__add__(right)
if isinstance(right, NoEscape):
return NoEscape(s)
return s


def escape_latex(s):
r"""Escape characters that are special in latex.

Expand Down Expand Up @@ -95,6 +71,30 @@ def escape_latex(s):
return NoEscape(''.join(_latex_special_chars.get(c, c) for c in str(s)))


class NoEscape(str):
"""
A simple string class that is not escaped.

When a `.NoEscape` string is added to another `.NoEscape` string it will
produce a `.NoEscape` string. If it is added to normal string it will
produce a normal string.

Args
----
string: str
The content of the `NoEscape` string.
"""

def __repr__(self):
return '%s(%s)' % (self.__class__.__name__, self)

def __add__(self, right):
s = super().__add__(right)
if isinstance(right, NoEscape):
return NoEscape(s)
return s


def fix_filename(path):
r"""Fix filenames for use in LaTeX.

Expand Down