diff --git a/bot/modules/Tex/core/LatexContext.py b/bot/modules/Tex/core/LatexContext.py index c8c2e7a0..88030da9 100644 --- a/bot/modules/Tex/core/LatexContext.py +++ b/bot/modules/Tex/core/LatexContext.py @@ -417,9 +417,9 @@ def parse_content(cls, content: str, mode: ParseMode): if mode == ParseMode.DOCUMENT: source = "\n\n".join(blocks) elif mode == ParseMode.GATHER: - source = "\n".join(["\\begin{{gather*}}\n{}\n\\end{{gather*}}".format(block) for block in blocks]) + source = "\n".join(["$\\begin{{gathered}}\n{}\n\\end{{gathered}}$".format(block) for block in blocks]) elif mode == ParseMode.ALIGN: - source = "\n".join(["\\begin{{align*}}\n{}\n\\end{{align*}}".format(block) for block in blocks]) + source = "\n".join(["$\\begin{{aligned}}\n{}\n\\end{{aligned}}$".format(block) for block in blocks]) elif mode == ParseMode.TIKZ: source = "\n".join(["\\begin{{tikzpicture}}\n{}\n\\end{{tikzpicture}}".format(block) for block in blocks]) else: diff --git a/bot/modules/Tex/core/LatexUserSetting.py b/bot/modules/Tex/core/LatexUserSetting.py index aa8853a5..b5b28ed1 100644 --- a/bot/modules/Tex/core/LatexUserSetting.py +++ b/bot/modules/Tex/core/LatexUserSetting.py @@ -241,7 +241,7 @@ def response(cls, ctx, data): if not data: return "The `tex` command will now render in paragraph mode, as usual." else: - return "The `tex` command will now render in maths mode, i.e., in a `gather*` environment." + return "The `tex` command will now render in maths mode, i.e., in a `gathered` environment." class alwayswide(LatexUserSetting, Boolean): diff --git a/bot/modules/Tex/core/tex_compile.py b/bot/modules/Tex/core/tex_compile.py index e087a7ea..1edea865 100644 --- a/bot/modules/Tex/core/tex_compile.py +++ b/bot/modules/Tex/core/tex_compile.py @@ -18,61 +18,47 @@ __location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__))) -def gencolour(colour, negate=True): +def gencolour(bgcolour, textcolour): """ - Build the colour conversion command for the provided colour, negating black text if required + Build the class options for the provided colourscheme """ - return r"convert {{image}} {} -bordercolor transparent -border 50 \ - -background {} -flatten {{image}}".format("+negate" if negate else "", colour) + return r", bgcolor={}, textcolor={}".format(bgcolour, textcolour) # Dictionary of valid colours and the associated transformation commands colourschemes = {} -colourschemes["white"] = gencolour("white", False) -colourschemes["black"] = gencolour("black") +colourschemes["white"] = gencolour("ffffff", "000000") +colourschemes["black"] = gencolour("000000", "ffffff") -colourschemes["light"] = gencolour("'rgb(223, 223, 233)'", False) -colourschemes["dark"] = gencolour("'rgb(20, 20, 20)'") +colourschemes["light"] = gencolour("dfdfdf", "1d1d1d") +colourschemes["dark"] = gencolour("141414", "eeeeee") -colourschemes["gray"] = colourschemes["grey"] = gencolour("'rgb(49, 51, 56)'") -colourschemes["darkgrey"] = gencolour("'rgb(35, 39, 42)'") +colourschemes["gray"] = colourschemes["grey"] = gencolour("313338", "ffffff") +colourschemes["darkgrey"] = gencolour("23272a", "ffffff") -colourschemes["trans_white"] = r"convert {image} +negate -bordercolor transparent -border 40 {image}" -colourschemes["trans_black"] = None +colourschemes["trans_white"] = gencolour("trans", "ffffff") +colourschemes["trans_black"] = gencolour("trans", "000000") colourschemes["transparent"] = colourschemes["trans_white"] colourschemes["default"] = colourschemes["grey"] -# Script which pads images to a minimum width of 1000 -pad_script = r""" -width=`convert {image} -format "%[fx:w]" info:` -minwidth=1000 -extra=$((minwidth-width)) - -if [ $extra -gt 0 ]; then - convert {image} \ - -gravity East +antialias -splice ${{extra}}x\ - -alpha set -background transparent -alpha Background -channel alpha -fx "i>${{width}}-5?0:a" +channel {image} -fi -""" # Header for every LaTeX source file -header = "\\documentclass[preview, border=20pt, 12pt]{standalone}\ - \n\\IfFileExists{eggs.sty}{\\usepackage{eggs}}{}\ +header = "\\IfFileExists{eggs.sty}{\\usepackage{eggs}}{}\ \n\\nonstopmode" """ # Alternative header to support discord emoji, but not other unicode -header = "\\documentclass[preview, border=20pt, 12pt]{standalone}\ - \n\\IfFileExists{eggs.sty}{\\usepackage{eggs}}{}\ +header = "\\IfFileExists{eggs.sty}{\\usepackage{eggs}}{}\ \n\\usepackage{discord-emoji} \n\\nonstopmode" """ # The format of the source to compile -to_compile = "{header}\ +to_compile = "\\documentclass[12pt, singlepage{colour}{alwayswide}]{{texit}}\ + \n{header}\ \n{preamble}\ \n\\begin{{document}}\ \n{source}\ @@ -102,18 +88,16 @@ async def makeTeX(ctx, source, targetid, preamble=default_preamble, colour="defa fn = "{}/{}.tex".format(path, targetid) with open(fn, 'w') as work: - work.write(to_compile.format(header=header, preamble=preamble, source=source)) + work.write(to_compile.format(colour=colourschemes[colour] or "", + alwayswide=", minpagewidth=110pt" if pad else "", + header=header, preamble=preamble, source=source)) work.close() # Build compile script script = ( "{compile_script} {id} || exit;\n" - "cd {path}\n" - "{colour}\n" - "{pad}").format(compile_script=compile_script_path, - id=targetid, path=path, - colour=colourschemes[colour] or "", - pad=pad_script if pad else "").format(image="{}.png".format(targetid)) + "cd {path}\n").format(compile_script=compile_script_path, + id=targetid, path=path).format(image="{}.png".format(targetid)) # Run the script in an async executor return await ctx.run_in_shell(script) diff --git a/bot/modules/Tex/resources/texcompile.sh b/bot/modules/Tex/resources/texcompile.sh index bdf44305..19243b07 100755 --- a/bot/modules/Tex/resources/texcompile.sh +++ b/bot/modules/Tex/resources/texcompile.sh @@ -23,7 +23,7 @@ then exit 1 fi -timeout 20 convert -density 700 -quality 75 -depth 8 -trim +repage $1.pdf -colorspace sRGB $1.png; +timeout 20 gs -q -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pngalpha -r1800 -dDownScaleFactor=3 -sOutputFile=$1.png $1.pdf if [ $? -eq 124 ]; then echo "Image processing timed out!"; diff --git a/bot/modules/Tex/tex_cmd.py b/bot/modules/Tex/tex_cmd.py index cf22766e..cb128e9f 100644 --- a/bot/modules/Tex/tex_cmd.py +++ b/bot/modules/Tex/tex_cmd.py @@ -34,8 +34,8 @@ async def cmd_tex(ctx, flags): is generally not required. Aliases:: tex: Code is compiled in the default `document` environment. - , or mtex: Code is rendered in math mode, in a `gather*` environment. - align: Code is rendered in math mode, aligned in an `align*` environment. + , or mtex: Code is rendered in math mode, in a `gathered` environment. + align: Code is rendered in math mode, aligned in an `aligned` environment. texsp: Same as `tex`, but ||spoiler|| the output image. texw: Don't pad the output (with transparent pixels) after compilation. tikz: Code is rendered in a `tikzpicture` environment.