4

I am using PHP on my server to create a latex table from a database, which is working fine. The problem I have now is getting that into my latex document. I thought that giving it the path would allow it to get the file from the web and input it like normal but that doesn't work and I've been unable to find anything online about doing this sort of thing.

Here is an example of what I'm trying to accomplish:

\documentclass{article}
\begin{document}
\input{http://example.com/generated-latex.php}
\end{document}
LEGEND383
  • 369
  • 1
    You can call wget from your LaTeX call, download the file, rename it and compile it. – Johannes_B Jun 10 '15 at 11:51
  • Sorry, but I don't quite get what you mean. I googled "latex wget" and found nothing. I'm using TeXworks on my windows laptop to compile the document (probably should have specified that, my bad). – LEGEND383 Jun 10 '15 at 12:05
  • wget is a command line tool to download stuff to the local harddrive. – Johannes_B Jun 10 '15 at 12:07
  • Was hoping for something other than downloading the file or copy/pasting from a web browser, like maybe an extension for the input command to allow web paths, but it looks like that's too much to ask for :( Oh well, thanks anyway – LEGEND383 Jun 10 '15 at 12:25
  • 1
    You need to somehow get this to TeX, a simple write to the commandline to download and read the file is the easiest it gets. If it is just for one or two occurrences, manually downloading will cause less of a headache. LuaTeX on the other hand might do it easier. – Johannes_B Jun 10 '15 at 12:29

1 Answers1

6

You could make use of the “piped \input” feature. For example, this is a MWE that inputs from the standard output of a cat command: the same idea can be applied to other commands, and, in principle, also to wget:

\begin{filecontents}{myTempFile.tex}
\section{A test}
This is a test.\par
\end{filecontents}

\documentclass[a4paper]{article} \usepackage[T1]{fontenc}

\begin{document} \input{|"cat myTempFile.tex"} \end{document}

Of course, for this example I have generated the temporary file by means of a filecontents environment.

Edit

By way of example, let us \input from the standard output of the date command as well:

\begin{filecontents}{myTempFile.tex}
\section{A test}
This is a test.\par
\end{filecontents}

\documentclass[a4paper]{article} \usepackage[T1]{fontenc}

\begin{document} \input{|"cat myTempFile.tex"} \input{|date} \end{document}

Here is the result:

Output of second example


Addition

Re-reading this answer, that I had posted more than a year ago, I realized that I had never completed it as I intended to. Just to convince yourself that the answer is “Yes, it is possible to input a file from the web”, try compiling a source file containing just the following, single line:

\input{|"curl --stderr /dev/null http://www.math.unipd.it/\string~mezzetti/Gustavo/Ludic/CDP/presentazione.tex"}

This example uses curl, but it is easy to adapt it to use wget instead. Remember that you have to enable the “full shell escape” feature, in order for this to work.

GuM
  • 21,558
  • So he could use the wget code wget -O - http://example.com/generated-latex.php to redirect wget to stdout and then use the pipe to insert it directly. – Juri Robl Jun 10 '15 at 12:50
  • @JuriRobl: Exactly. Btw, I’m not very familiar with wget, but is it -o or -O the option for redirecting the output to a file (to stdout, in case - is given as the file name)? EDIT: forget my question, I’ve just checked: -o is for redirecting the log messages. – GuM Jun 10 '15 at 12:55
  • Yes, I think it should work that way (had to look at the docs, last time I used it was some time ago). – Juri Robl Jun 10 '15 at 12:57
  • I tried \input{|date} and got the error that it couldn't find the file "|date", am I missing something? – LEGEND383 Jun 10 '15 at 13:20
  • Nevermind, found the switch to enable pipes and a windows version of wget – LEGEND383 Jun 10 '15 at 13:58
  • @LEGEND383: The example won't work unless you enable the shell-escape feature; but it seems that you have already found how to do that. – GuM Jun 10 '15 at 15:18
  • 1
    It might be worth pointing out that doing this with anything other than a trusted, secure file would be An Extremely Bad Idea Indeed. Running cat on an arbitrary local file is already risky, of course. – cfr Aug 03 '17 at 22:24
  • @cfr: More generally, enabling the “full shell escape” feature is a Very Bad Idea in itself. – GuM Aug 03 '17 at 22:29
  • @GuM It is very useful, though. But I would only use it with code I wrote or have examined carefully. I would not use it with remote code unless I could be absolutely positive it was mine. (And it is really hard to see how I could be positive of that.) – cfr Aug 03 '17 at 22:41
  • Is this not possible on Overleaf? – pretzlstyle Nov 03 '20 at 21:18