Skip to content

Commit

Permalink
clean repo for latex-cli (flashcards moved to other repo)
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanlct committed Feb 29, 2020
1 parent 6894507 commit 8b20e95
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 71 deletions.
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
Command-line flashcard tool
# Display LaTeX in the terminal

# Dependencies
Generate an image from a LaTeX code and displays it in the terminal.

## Example

![Example usage](./example.png "Example usage")

## Dependencies

- pdflatex
- imagemagick
- imgcat (for OS X users) / tycat for Linux users but not supported yet (make sure imgcat/tycat is in path)
eg for imgcat in iterm2 i had to add
export PATH=/Users/username/.iterm2:$PATH
- imgcat

## Note

The `imgcat` package works with iTerm2 (macOS) or with tmux. Could look into the `tycat` package to display images on Linux.

In iTerm2, I had to do `export PATH=/Users/username/.iterm2:$PATH` after installing `imgcat`.

The size and quality of the generated images can be adjusted in the code via the parameters `density`, `quality` and `resize`.

## Todo

- Add an option to just generate the image without displaying it
- Add an argument parser to play with the parameters for generating the image
- Add an option to include LaTeX packages (eg. to generate tables, plots, figures...)
- Add Linux support for image display
- Options to reuse previous command or correct it
- Support for new commands (`\R`, `\f` ...)
Binary file added example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 23 additions & 66 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import subprocess
import time
import tempfile


Expand All @@ -13,86 +12,44 @@
\end{document}"""


class tcolor:
class fg:
DEFAULT = '\033[39m'
BLACK = '\033[30m'
RED = '\033[31m'
GREEN = '\033[32m'
YELLOW = '\033[33m'
BLUE = '\033[34m'
MAGENTA = '\033[35m'
CYAN = '\033[36m'
LIGHT_GRAY = '\033[37m'
DARK_GRAY = '\033[90m'
LIGHT_RED = '\033[91m'
LIGHT_GREEN = '\033[92m'
LIGHT_YELLOW = '\033[93m'
LIGHT_BLUE = '\033[94m'
LIGHT_MAGENTA = '\033[95m'
LIGHT_CYAN = '\033[96m'
WHITE = '\033[97m'

class bg:
DEFAULT = '\033[49m'
BLACK = '\033[40m'
RED = '\033[41m'
GREEN = '\033[42m'
YELLOW = '\033[43m'
BLUE = '\033[44m'
MAGENTA = '\033[45m'
CYAN = '\033[46m'
LIGHT_GRAY = '\033[47m'
DARK_GRAY = '\033[100m'
LIGHT_RED = '\033[101m'
LIGHT_GREEN = '\033[102m'
LIGHT_YELLOW = '\033[103m'
LIGHT_BLUE = '\033[104m'
LIGHT_MAGENTA = '\033[105m'
LIGHT_CYAN = '\033[106m'
WHITE = '\033[107m'

class style:
RESET = '\033[0m'
BOLD = '\033[1m'
DIM = '\033[2m'
UNDERLINED = '\033[4m'
BLINK = '\033[5m'
REVERSE = '\033[7m'
HIDDEN = '\033[8m'
RESET_BOLD = '\033[21m'
RESET_DIM = '\033[22m'
RESET_UNDERLINED = '\033[24m'
RESET_BLINK = '\033[25m'
RESET_REVERSE = '\033[27m'
RESET_HIDDEN = '\033[28m'


def exec(cmd, capture_output=True):
log = subprocess.run(cmd, capture_output=capture_output)
if log.returncode:
print('Error when executing command')
print('\nAn error occured when running the command "' + cmd[0] + '"')
if capture_output:
print(tcolor.fg.LIGHT_YELLOW + log.stdout.decode('utf-8') + tcolor.fg.DEFAULT)
print("This is the error message:\n")
print(log.stdout.decode('utf-8'))
return log.returncode == 0


def display_tex(latex):
# generate a temporary directory
with tempfile.TemporaryDirectory() as tmpdirname:
filename = f'{tmpdirname}/out'

# create a .tex file
with open(f'{filename}.tex', 'w') as f:
f.write(LATEX_TEMPLATE % latex)

latex_cmd = ['pdflatex', '-interaction=nonstopmode', f'-output-directory={tmpdirname}', f'{filename}.tex']
convert_cmd = ['convert', '-quiet', '-density', '3000', '-quality', '90', '-alpha', 'off', '-background', 'white', '-resize', '15%', f'{filename}.pdf', f'{filename}.png']
latex_cmd = ['pdflatex', '-interaction=nonstopmode',
f'-output-directory={tmpdirname}', f'{filename}.tex']
convert_cmd = ['convert', '-quiet', '-density', '2000', '-quality', '90',
'-alpha', 'off', '-resize', '25%', f'{filename}.pdf', f'{filename}.png']
display_cmd = ['imgcat', f'{filename}.png']

# generate a .pdf from the .tex
if exec(latex_cmd):
# generate a .png from the .pdf
if exec(convert_cmd):
# display the .png
exec(display_cmd, capture_output=False)

print(tcolor.bg.WHITE + tcolor.fg.BLACK + 'What does this formula mean?' + tcolor.fg.DEFAULT + tcolor.bg.DEFAULT + '\n')
exec(latex_cmd)
exec(convert_cmd)
exec(display_cmd, capture_output=False)


if __name__ == "__main__":
display_tex(r'\displaystyle\bigcup_{t\in \mathbb{N}} \left\{\frac{1}{t^6}\right\}')
while True:
str_in = input("Enter LaTeX: ")
if not str_in:
break
print()
display_tex(str_in)
print()

0 comments on commit 8b20e95

Please sign in to comment.