我的问题来自this one。但是,不同之处在于我的输出是PDF。
我在外部文件中保存了C++
代码。我想将其打印为带有语法高亮的r markdown PDF。
我的example.cpp
代码,实际上是TMB
代码:
// Fitting Bivariate Gaussian distribution.
#include <TMB.hpp>
template<class Type>
Type objective_function<Type>::operator() ()
{
using namespace density;
DATA_MATRIX(Y);
PARAMETER_VECTOR(rho);
PARAMETER_VECTOR(sigma);
vector<Type> rho_temp(1);
rho_temp = rho;
vector<Type> sigma_temp(2);
sigma_temp = sigma;
Type res;
for(int i = 0; i < 50; i++)
res += VECSCALE(UNSTRUCTURED_CORR(rho_temp), sigma_temp)(Y.row(i));
return res;
}
最小代码:
---
title: "Code to PDF"
output: beamer_presentation
safe-columns: true # enables special latex macros for columns
header-includes:
- \usepackage{listings}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
setwd("/home/guilherme/Google Drive/Mestrado/dissertacao/TMB/Presentation")
```
## Slide1
\lstinputlisting[language=C++]{example.cpp}
幻灯片中的结果:
是否有更好的方法来突出显示?
Parreira。
您可以在Rmarkdown中运行很多引擎。您可以找到它们here。
一般情况下:
我的C ++档案,我命名为'mycpp.cpp':
# include <iostream>
class Passaro // classe base
{
public:
virtual void MostraNome()
{
std::cout << "um passaro";
}
virtual ~Passaro() {}
};
class Cisne: public Passaro // Cisne é um pássaro
{
public:
void MostraNome()
{
std::cout << "um cisne"; // sobrecarrega a função virtual
}
};
int main()
{
Passaro* passaro = new Cisne;
passaro->MostraNome(); // produz na saída "um cisne", e não "um pássaro"
delete passaro;
}
我的Rmd存档:
---
title: "Code to PDF"
output:
pdf_document
---
# Cats are nicer than dogs
```{Rcpp, code=readLines('mycpp.cpp')}
```
输出:
针对您的情况,请尝试以下操作:
---
title: "Code to PDF"
output: beamer_presentation
safe-columns: true # enables special latex macros for columns
header-includes:
- \usepackage{listings}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
setwd("/home/guilherme/Google Drive/Mestrado/dissertacao/TMB/Presentation")
```
## Slide1
```{Rcpp, eval = FALSE, echo = TRUE, code=readLines('example.cpp')}
```
最佳Vinicius。