8

I mean C++ doing some calculation not inserting the code as a text.

7
  • 3
    No: why would you think it would be? What are you actually trying to calculate?
    – Joseph Wright
    Commented Jan 26, 2015 at 20:44
  • 5
    With --shell-escape active I guess you could do whatever you want to, all of which would be external to (La)TeX.
    – Werner
    Commented Jan 26, 2015 at 20:51
  • 3
    You might be interested in LuaLaTeX and get your calculations done with Lua.
    – Johannes_B
    Commented Jan 26, 2015 at 20:59
  • 1
    If you're talking about mathematical calculations on the fly then sagetex is more applicable than a programming language. But it gives you the ability to program in Python, too. Sagetex al­lows you to em­bed code, the re­sults of com­pu­ta­tions, plots, and graph­ics into your document. So $\sage{5*3}$ would show up as 15 in your document.
    – DJP
    Commented Jan 26, 2015 at 23:52
  • 2
    You could insert C++ code, but to do calculations on the fly you would need to write the code from inside TeX to file, call the compiler from inside TeX and execute the compiled programme from inside TeX. The C++ programme needs to write its output to a file such that it can then be read by TeX. Commented Jan 27, 2015 at 6:39

2 Answers 2

11

In this solution I use the facilities of fancyvrb to write the C++ source code to a file called hello.cpp. Then I proceed to compile the written file and execute the programme. Finally I include the output generated by the programme.

You need --shell-escape for the example to work.

\documentclass{article}
\usepackage{fancyvrb}
\begin{document}

\begin{VerbatimOut}{hello.cpp}
#include <fstream>

int main() {
  std::ofstream out;
  out.open("cpp-out.txt");

  out << "Hello World!" << std::endl;

  out.close();
  return 0;
}
\end{VerbatimOut}

\immediate\write18{g++ -o hello hello.cpp}
\immediate\write18{./hello}

\input{cpp-out.txt}

\end{document}

enter image description here

Other example

We can use this technique to include C code in a similar way (of course we need to use another compiler). In this example I exploit this to rapidly calculate factorials:

\documentclass{article}
\usepackage{fancyvrb}
\begin{document}

\begin{VerbatimOut}{fac.c}
#include <stdio.h>

long long factorial(int n) {
  if (n <= 1) return 1;
  return n*factorial(n-1);
}

int main(int argc, char* argv[]) {
  int n = atoi(argv[1]);
  printf("%lld\n", factorial(n));
  return 0;
}
\end{VerbatimOut}

\immediate\write18{gcc -o fac fac.c}

\newcommand\factorial[1]{%
  \immediate\write18{./fac #1 > /tmp/result.tex}%
  \input{/tmp/result.tex}%
}

\factorial{5}

\factorial{20}

\end{document}

enter image description here

Now the same code with lualatex, which will yield the same output:

\documentclass{article}
\usepackage{luacode}
\begin{luacode*}
function factorial(n)
  if (n <= 1) then
    return 1
  end
  return n*factorial(n-1)
end
\end{luacode*}
\begin{document}

\newcommand\factorial[1]{%
  \luaexec{tex.sprint(string.format("\%d", factorial(#1)))}%
}

\factorial{5}

\factorial{20}

\end{document}
6
  • How to install lualatex? I think, this is not a simple package! I cannot run this code Commented Jan 27, 2015 at 15:01
  • Install an up to date Texlive (Windows, Linux) or MacTex (Mac). Do not use the packages supplied by a package manager, this are mostly outdated.
    – MaxNoe
    Commented Jan 27, 2015 at 15:27
  • I am using Texmaker editor on OSX system. I also have a so called TexLiveUtility installed on the OSX. This is what I need? Commented Jan 27, 2015 at 20:26
  • @user3058699 Open a terminal and try to typeset the file with lualatex file.tex. If it fails with "Command not found" or similar, then you need to go to TeXlive utility, search for luatex or lualatex and install it. Also do us a favour and change your username to something more telling than "user3058699". Commented Jan 28, 2015 at 8:01
  • I have the command. Commented Jan 30, 2015 at 13:54
5

No, you cannot have the TeX program itself process C++ source code directly except to typeset it.

C++ is a compiled language, so the source code must be translated to assembler, assembled, and linked by a separate compiler to create an executable binary.

TeX is an interpreted language in which the TeX program acts as a "document compiler", so it interprets the source code and outputs not a binary executable but (originally) a DVI file format from which a document can be produced. It's not capable of compiling C++ code on its own.

You could invoke a C++ executable program from within a TeX document by using --shell-escape when you compile the TeX document. That executable could be originally compiled from any programming language; it has nothing to do with TeX.

I suppose you could write the C++ code in a TeX file, have TeX write out the code to a separate file, invoke the C++ compiler with --shell-escape, and then invoke the resulting executable (assuming successful compilation) -- but why?

If you want to script together the results of multiple independent programs, use Bash or something else that was designed for such a purpose.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .