Skip to content

Instantly share code, notes, and snippets.

@ayaderaghul
Last active August 29, 2015 14:03
Show Gist options
  • Save ayaderaghul/58d3f837ba545e06c9af to your computer and use it in GitHub Desktop.
Save ayaderaghul/58d3f837ba545e06c9af to your computer and use it in GitHub Desktop.
CSV -> Latex table syntax
;; this inputs a csv file and produces table syntax in latex
;; file name sample to enter: gni.csv
;; it outputs automatically
;; note: csv file needs to be produced like this in libre:
;; the separator should be & (latex style) instead of ; or ,
;; any commas left in the csv file should be switched to space or -
;; to avoid confusion because Racket reads csv with commas as separator
;; note: i leave the fixed number of columns to be 10 ^^ (for laziness)
;; that doesnt cause much trouble in latex though
(require racket/gui/base)
(require 2htdp/batch-io)
(define frame (new frame% [label "tabular"]))
(define temp-file "gni.csv")
(define (current-file) (send csv-input get-value))
(define (csv-entry)
(with-handlers
([exn:fail? (lambda (v) "...")])
(read-csv-file temp-file)))
(define temp-entry (csv-entry))
(define (csv-content)
(eval
(append
(list 'format
(eval
(append
(list 'string-append "\\begin{tabular}{lccccccccc} \n")
(for/list ([i (length temp-entry)])
"~a \\\\ \n")
(list "\\end{tabular} \\\\ \n \n"))))
(for/list ([i (length temp-entry)])
(first (list-ref temp-entry i))))))
(define temp-content (csv-content))
(define csv-input
(new text-field%
[parent frame]
[label "Enter file name here: "]
[init-value ""]
[stretchable-width #t]
[callback (lambda (b e)
(set! temp-file (current-file))
(set! temp-entry (csv-entry))
(if (equal? temp-entry "...")
(update-error-msg!)
(begin
(update-hint-msg!)
(set! temp-content (csv-content))
(with-output-to-file
"csv-output.txt"
(lambda () (printf temp-content))
#:exists 'append))))]))
(define error-msg (new message%
[parent frame]
[stretchable-width #t]
[label ""]))
(define (update-error-msg!)
(send error-msg set-label "ERROR: not a file"))
(define (update-hint-msg!)
(send error-msg set-label ""))
(send frame show #t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment