@@ -2074,10 +2074,10 @@ If we were writing a Python module to write TeX, we might do something like this
2074
2074
the environments are closed properly::
2075
2075
2076
2076
>>> def start(env):
2077
- ... return '\begin{}'.format(env)
2077
+ ... return '\\ begin{{{}} }'.format(env)
2078
2078
2079
2079
>>> def end(env):
2080
- ... return '\end{}'.format(env)
2080
+ ... return '\\ end{{{}} }'.format(env)
2081
2081
2082
2082
>>> def may_error():
2083
2083
... import random
@@ -2102,18 +2102,18 @@ Function Based Context Managers
2102
2102
-------------------------------
2103
2103
2104
2104
To create a context manager with a function, decorate with
2105
- ``contextlib.contextmanager ``, and yield where you want to bookend ::
2105
+ ``contextlib.contextmanager ``, and yield where you want to insert your block ::
2106
2106
2107
2107
>>> import contextlib
2108
2108
>>> @contextlib.contextmanager
2109
2109
... def env(name, content):
2110
- ... content.append(r'\ begin{}'.format(name))
2110
+ ... content.append('\\ begin{{{}} }'.format(name))
2111
2111
... try:
2112
2112
... yield
2113
2113
... except ValueError:
2114
2114
... pass
2115
2115
... finally:
2116
- ... content.append(r'\ end{}'.format(name))
2116
+ ... content.append('\\ end{{{}} }'.format(name))
2117
2117
2118
2118
Our code looks better now, and there will always be a closing tag::
2119
2119
@@ -2122,7 +2122,7 @@ Our code looks better now, and there will always be a closing tag::
2122
2122
... out.append(may_error())
2123
2123
2124
2124
>>> out
2125
- ['\\begincenter ', 'content', '\\endcenter ']
2125
+ ['\\begin{center} ', 'content', '\\end{center} ']
2126
2126
2127
2127
Class Based Context Managers
2128
2128
----------------------------
@@ -2135,14 +2135,14 @@ To create a class based context manager, implement the ``__enter__`` and ``__exi
2135
2135
... self.content = content
2136
2136
...
2137
2137
... def __enter__(self):
2138
- ... self.content.append(r'\ begin{}'.format(
2138
+ ... self.content.append('\\ begin{{{}} }'.format(
2139
2139
... self.name))
2140
2140
...
2141
2141
... def __exit__(self, type, value, tb):
2142
2142
... # if error in block, t, v, & tb
2143
2143
... # have non None values
2144
2144
... # return True to hide exception
2145
- ... self.content.append(r'\ end{}'.format(
2145
+ ... self.content.append('\\ end{{{}} }'.format(
2146
2146
... self.name))
2147
2147
... return True
2148
2148
@@ -2153,7 +2153,7 @@ The code looks the same as using the function based context manager::
2153
2153
... out.append(may_error())
2154
2154
2155
2155
>>> out # may_error had an issue
2156
- ['\\begincenter ', '\\endcenter ']
2156
+ ['\\begin{center} ', '\\end{center} ']
2157
2157
2158
2158
2159
2159
Context objects
0 commit comments