Skip to content

Instantly share code, notes, and snippets.

@caugner
Created February 22, 2017 22:41
Show Gist options
  • Select an option

  • Save caugner/d2b4b6263306e989364604472470b835 to your computer and use it in GitHub Desktop.

Select an option

Save caugner/d2b4b6263306e989364604472470b835 to your computer and use it in GitHub Desktop.
Visualise bracket depth of a LaTeX document.
# Usage:
# $ cat file.tex | ruby latex-bracket-depth.rb
# $ echo "\textbf{ \textit{foobar} }" | ruby latex-bracket-depth.rb
# \textbf{1\111111{222222}1}
content = STDIN.read
len = content.size
level = 0
i = 0
esc = false
while i < len
c = content[i]
if esc
esc = false
elsif c == '{'
level += 1
elsif c == '}'
level -= 1
elsif c == '\\'
esc = true
end
if level != 0 and not c =~ /[{}\\]/
print level
else
print c
end
i += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment