edelans
edelans

Reputation: 9088

store Latex in Json format

I am building a math exercises database, I would like to use couchDB and then store the exercises in a json format, so I can store meta-data with each exercise.

I have troubles having a valid json with all the Latex syntax, what would be a good way to tackle this issue? I have thought about encoding the latex but I am not sure it's a good idea, above all if it will need to be decoded by a mobile device...

Example of data:

     {"taglist": null,
     "flagcount": null,
     "category": "Algebre",
     "chapter": "Polynomes",
     "difficulty": 1,
     "viewcount": null,
     "hint": null,
     "question": "Soit $P \in \mathbb{R}[X]$ scindé sur $\mathbb{R}$.\\ \begin{enumerate} \item Montrer que $P'$ est aussi scindé sur $\mathbb{R}$. \item Montrer que les racines multiples de $P'$ sont aussi racines de $P$. \item Ce resultat reste-t-il valable dans $\mathbb{C}[X]$ ? \end{enumerate}"
     "solution": null}

Moreover, as I might need to encrypt the "solution" because I do no want it to be accessed without permission, maybe I should store only the encrypted form in the json?

Or maybe the solution is to store the latex another way... ? I am very new to this kind of problem, I appreciate any help ;).

Thanks

Upvotes: 4

Views: 3072

Answers (2)

Marcin Skórzewski
Marcin Skórzewski

Reputation: 2854

In JSON strings backslash character \ have special meaning. If you want to store it in your data use \\ (escaped backslash). JSON parser will change it back to the single one and Couch should store "$\\mathbb{R}$.\\\\" as $\mathbb{R}$.\\.

Upvotes: 5

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33046

As long as you correctly quote the LaTeX string, I don't see any problem here. There are great in-browser LaTeX renderers, have a look to http://math.stackexchange.com, for instance. Only, keep in mind that LaTeX rendering can be a heavy process.

Good news is that you have a function in the Python standard library which will handle encoding for you: json.dumps. Just pass it a dictionary with all the data above. You can also extend the underlying encoder to accept your data types.

As of hiding data, I suggest not to send answer with questions, as a determined attacker could find the passphrase anyway by digging in your JS. Instead, have the client submit answers via XHR. If the answer is correct, send back a confirmation with explanation. If it isn't, send back a hint or something.

You can use a timer and a counter to make the user wait between two submissions for the same problem (so that he/she has to think, instead of sending lots of random values hoping to hit the solutions) and give them the solutions anyway after a certain number of wrong submitted answers.

Upvotes: 3

Related Questions