Skip to content
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
4 changes: 2 additions & 2 deletions bot/modules/Tex/core/LatexContext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion bot/modules/Tex/core/LatexUserSetting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
56 changes: 20 additions & 36 deletions bot/modules/Tex/core/tex_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}\
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion bot/modules/Tex/resources/texcompile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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!";
Expand Down
4 changes: 2 additions & 2 deletions bot/modules/Tex/tex_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down