Export C source code and results in org-mode - org-mode

I have the following block of code in my org file:
#+begin_src C -n :exports both :results output
#include <stdio.h>
int main()
{
printf("This will work!");
return 0;
}
#+end_src
#+RESULTS:
When I compile to a PDF the only result is the "This will work" string. Its only half of what I want. I want the source code and the result to be presented in the PDF. Can anyone tell me where i'm going wrong?
Im using emacs on Mac osX with a org version of 7.9.3

Related

How to make Emacs autofill code for c files

I'm completely new to Emacs and I'm looking for a way to make Emacs automatically write these lines of code for C files
#include <stdio.h>
int main()
{
return 0;
}
I'm sorry for messing up these lines, but I think you've got the point.
Yasnippet is perfect for re-usable snippets .
Have a look at the emacs wiki
Install using: M-x package-install yasnippet, or look on project on github.
Once installed, enable yasnippet-minor-mode with M-x yas-minor-mode-on (when your C file is open, the buffer's major mode is on c-mode and yas-minor-mode adds yas functionality.)
C-c&C-n will open a buffer for a snippet.
Give it a name and copy your code into the body, result should look like:
# key: c
# name: c_include
# --
#include <stdio.h>
int main()
{
return 0;
}
C-cC-c to close and save the buffer.
It will now appear with the name you gave it in the yasnippet menu when you call
C-c&C-s
Yasnippet has a lot more functionality. Read the doc.
Find C templates in AndreaCrotti yasnippet snippets Github repository

Can company-mode complete my code in org-mode?

I need to edit source code in org-mode.
#+BEGIN_SRC cpp
void elimDups(vector<string> &words)
{
sort(words.begin(), words.end());
auto end_unique = unique(words.begin(), words.end());
words.erase(end_unique, words.end());
}
#+END_SRC
In code blocks, there is no code completion.
Is it possible to use code completion in code blocks in org-mode?
You can do C-c ' to open a new window with the corresponding major mode.
Then you have code completion.

How do I get org-mode to execute code blocks consistently

I'm running R code blocks in a session in an org-mode file, and I can usually get them to execute by C-c C-c, as expected.
I think the pertinent PROPERTY lines I've put at the top of my file are
#+PROPERTY: session *R*
#+PROPERTY: cache yes
#+PROPERTY: exports both
#+PROPERTY: tangle yes
Sometimes, nothing happens when I press C-c C-c. AFAICT, that's a seemingly random event; the same code block may work one time and fail another time.
When such a block fails, I do see the results echoed to the minibuffer, but I don't see any results (e.g., a new data frame, as I would have expected in many cases) in the R session.
If I press C-' to edit the code block and then press C-j on each line (or C-r on each region), the code does execute, and the results get echoed appropriately to the org file and show up in the R session.
Here are some sample code block begin lines:
#+begin_src R :results silent :exports code
#+begin_src R :results value :colnames yes :exports both
I tested both just now by doing rm(list=ls()), running the code blocks, and doing ls() in the session to see if the results were there.
The first block is simple:
#+begin_src R :results silent :exports code
require(stringr)
require(ggplot2)
require(scales)
require(arm)
require(YaleToolkit)
require(stinepack)
require(mixtools)
require(lubridate)
source("utilities.R")
pf <- function (x,y) {
z <- sqrt(x * x + y * y)
return(x/z)
}
#+end_src
In two tests, it worked once and failed once, as detected by looking for pf in the R session.
If important, I can try to produce an ECM that fails frequently enough to be useful, but I suspect this may be a common setup or version problem that someone has seen and solved. It seems related to Org-mode code block evaluation, but the solution there seems to have been to :export results. In most cases, I use :exports both, which would seem to cover that case, and, in at least some cases, I care about setting up the environment in the session and not exporting to the org file. I'm never passing data through org-mode; I'm only using the session for that.
BTW, I'm running org-mode 7.8.03 on GNU Emacs 23.3.1 on XP Pro 64.

How to make the powershell line-buffered in emacs for Windows

I want to use powershell in emacs, but it seems that the powershell in emacs is block buffered. For example, when I write the a simple c program like this:
int main()
{
printf("input the number of a value: \n");
scanf("%d", &num);
}
I compile it and make it run in Powershell under emacs. It won't print out the line input the number of a value: until I input a number and hit Enter. the c program runs well in powershell outside the emacs. My question is how can I run the PowerShell line buffered in emacs?
edit I use the Powershell.el
The stdout of the C library is line buffered if the stdout of your program is connected to a terminal. The reason you find the behavior in your original program (without fflush) is simply because while running through the GNU EMACS, your program thinks it is not connected to a real terminal. Experiment running your program in M-x term or M-x ansi-term, for example. The solutions given here suggest you using fflush, which is definitely not a solution as you won't change every little program that uses the C library in your system. So the answer to your question is really giving your GNU EMACS a real terminal and not changing your program. Now, the bad news is that neither of these terminals for the GNU EMACS seem to work on Windows. (Sorry.) (Nevertheless, it appears someone claims to run ansi-term on Windows.)
You need to fflush before the fscanf.
This works for me with eshell:
#include <stdio.h>
int main() {
int num;
printf("input the number:\n");
fflush(stdout);
scanf("%d", &num);
printf("inc: %d\n", num + 1);
}
I couldn't reproduce your problem with powershell.el because I see another: it doesn't wait for the input, "reads" a 0 instead, prints "inc: 1" and exits.

noweb style weaving in org-babel

I'm using Emacs 23 with Org 7.8.04. My code structure is as follows:
#+TITLE: hello, world!
#+BEGIN_SRC python :tangle yes :noweb yes
<<go_function>>
if __name__ == "__main__":
go()
#+END_SRC
Define =go_function= as follows.
#+name:go_function
#+BEGIN_SRC python
def go:
print "hello, world!"
#+END_SRC
When I tried to weave documentation, the <<go_function>> in the first code chunk is exported to html too so that I have two html exports of <<go_function>>. I'd like the <<go_function>> is exported as a link which points to the actual definition at the end of the document.How can I do that?
Try changing :noweb yes to :noweb tangle. The manual is very helpful in cases like this (see https://orgmode.org/manual/Noweb-Reference-Syntax.html).