aboutsummaryrefslogtreecommitdiffstats
path: root/bot
diff options
context:
space:
mode:
authorGravatar Shakya Majumdar <[email protected]>2021-04-08 23:45:35 +0530
committerGravatar Shakya Majumdar <[email protected]>2021-04-08 23:45:35 +0530
commit6b6e2bd0ec58197a88ca9fdc8cc3eeccbf8ebab5 (patch)
tree4673d847b0b13dca5a3f9e52f02570239bb8bcd1 /bot
parentMerge pull request #637 from python-discord/dependabot/pip/pyyaml-5.4 (diff)
Add latex cog
Diffstat (limited to 'bot')
-rw-r--r--bot/exts/evergreen/latex.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/bot/exts/evergreen/latex.py b/bot/exts/evergreen/latex.py
new file mode 100644
index 00000000..79b50ccd
--- /dev/null
+++ b/bot/exts/evergreen/latex.py
@@ -0,0 +1,57 @@
+from io import BytesIO
+from typing import Union
+
+import discord
+import matplotlib.pyplot as plt
+from discord.ext import commands
+
+# configure fonts and colors for matplotlib
+plt.rcParams.update(
+ {
+ "font.size": 16,
+ "mathtext.fontset": "cm", # Computer Modern font set
+ "mathtext.rm": "serif",
+ "figure.facecolor": "38383F", # matches Discord's dark mode background color
+ "text.color": "white",
+ }
+)
+
+
+class Latex(commands.Cog):
+ """Renders latex."""
+
+ def __init__(self, bot: commands.Bot):
+ self.bot = bot
+
+ @staticmethod
+ def _render(text: str) -> Union[BytesIO, str]:
+ """Return the rendered image if latex compiles without errors, otherwise return the error message."""
+ text = text.replace(r"\\", "$\n$") # matplotlib uses \n for newlines, not \\
+ fig = plt.figure()
+
+ try:
+ fig.text(0, 1, text, horizontalalignment="left", verticalalignment="top")
+
+ rendered_image = BytesIO()
+ plt.savefig(rendered_image, bbox_inches="tight", dpi=600)
+ rendered_image.seek(0)
+ return rendered_image
+
+ except ValueError as e:
+ return str(e)
+
+ @commands.command()
+ async def latex(self, ctx: commands.Context, *, text: str) -> None:
+ """Renders the text in latex and sends the image."""
+ async with ctx.typing():
+ image = self._render(text)
+
+ if isinstance(image, BytesIO):
+ await ctx.send(file=discord.File(image, "latex.png"))
+ else:
+ await ctx.send(image)
+
+
+def setup(bot: commands.Bot) -> None:
+ """Load the Latex Cog."""
+ bot.add_cog(Latex(bot))