2

I want to know if it's possible to get data from a website using api json in a document .tex

I want to do this without luatex because the idea is compile online with the sharelatex website, that does't support luatex.

Best regards

  • yes, but I understand that he just uses luacode package to generate data, but not gettting data from a url – Sara Rodriguez Oct 11 '15 at 18:59
  • 3
    Sharelatex does support luatex. You can select which compiler to use in the project settings. However, I doubt it will let your document to make socket connections (I don't even think that lualatex implements the socket api, but of course much less pure latex could do that) – JLDiaz Oct 11 '15 at 19:02
  • You can do it without LuaTeX, I'm sure, because you can use any external tool you like. But you are not going to be able to do it using an online compilation service worth its salt, because any sane service will prohibit users from running external stuff. You'd need to enable shell escape and no sane webmaster is going to let you do that on their server. To put this another way: if ShareLaTeX did let you do this, that would be a reason to run away from that service fast because it would mean it was in the hands of idiots. Fortunately, I'm sure they aren't idiots as uptime exceeds 30 secs. – cfr Oct 11 '15 at 21:37
5

I would recommend against trying to import data directly into LaTeX from a REST/JSON API. This would in any case require outside tools as LaTeX cannot open network connections. I'd suggest to use a language like Python or Ruby to query the API and to transform the JSON data into something that is easier to process with LaTeX.

A great tool for transforming JSON data is jq - I'm using it to trim down JSON data to the bits that I need in a document in combination with Curl to retrieve the data from a REST API.

Here is code using Curl, jq, and the API from above. It emits

\geo{37.4220352}%
     {-122.0841244}

which would be easy to parse in LaTeX by creating a command \geo.

#! /bin/sh

URL="http://216.58.210.138/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false"

curl -s "$URL" | jq -r '"
\\geo{\(.results[0].geometry.location.lat)}%
     {\(.results[0].geometry.location.lng)}
"'
6

As I hinted in a comment, even if Sharelatex allows to use lualatex as compiler for your documents, it does not allow network connections from luatex code. I performed the following test:

  1. Wrote the following testing file, which attempts a connection to Google Geocoding API to get a JSON containing geocoding information about the location "1600 Amphitheatre Parkway, Mountain View, CA". Then, it uses lualibs to convert that JSON into lua tables, and then the "lat" and "lng" of the geolocation components are printed in the document.

    \documentclass{article}
    \usepackage{luacode}
    
    \begin{document}
    \begin{luacode}
    local http = require("socket.http")
    require("lualibs.lua")
    
    r, c, h = http.request ("http://216.58.210.138/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false")
    
    if r then
       local s =  utilities.json.tolua(r)
       tex.print("Success! $\\to$ (")
       tex.print(tostring(s["results"][1]["geometry"]["location"]["lat"]), ", ")
       tex.print(tostring(s["results"][1]["geometry"]["location"]["lng"]), ")")
    else
       tex.print("ERROR: ")
       tex.print(c)
    end
    \end{luacode}
    \end{document}
    

    When run locally, this works and produces a document containing:

    Successful result

  2. Upload above code to Sharelatex, and ensured that lualatex compiler was in use (as shown in next screenshot)

    Sharelatex settings

  3. Compiled this example in Sharelatex, but got a document showing an error:

    Network error

So, as expected, Sharelatex does not allow network connections.

  • FYI, I tried the same test in Overleaf (another online latex engine), with the same unsuccessful result. – JLDiaz Oct 12 '15 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.