My question is inspired from this one. However, the difference is that my output is PDF.
I have a C++
code saved in an external file. I want to print it into a r markdown PDF with syntax highlight.
My example.cpp
code, which is in fact a TMB
code:
// 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;
}
Minimal code:
---
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}
result in the slide:
Is there a better way to do this highlighting?
\lstinputlisting[language=C++]{example.cpp}
. Maybe with some ```{latex}... around it. Can you make a minimal reproducible example to test? – Kubiak