Knitr option hook results in LaTeX output - knitr

I'm creating ioslides and beamer output from RMarkdown source files and need to have variable figure output dependent on the output format.
I've generate a plot using ggplot2 which renders fine.
I want the plot to have an out.width set to 100% for HTML output and 70% for LaTeX output.
The problem is that when I set the option hook and check for LaTeX output, the tex file generated by knitr contains the LaTeX source verbatim for including the image which renders as text in the slide.
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '70%'
}
options
})
The plot renders fine in HTML output.
However, for beamer I get as shown in the image:
And the resulting output in the .tex file:
\begin{frame}{Why bother?}
\protect\hypertarget{why-bother}{}
\textbackslash begin\{center\}\textbackslash includegraphics{[}width=70\% {]}\{slide-book\_files/figure-beamer/lec-4-modularity-cost-1\}
\textbackslash end\{center\}
\end{frame}
Here's complete code for a MWE:
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '70%'
}
return (options)
})
```{r, lec-4-modularity-cost, echo=FALSE, out.width='100%', fig.align='center'}
plot.data <- tibble (X = seq (0,100), Y = 2 * X)
ggplot(plot.data, aes(x=X, y=Y)) +
geom_path()
```

When setting out.width this way you have to use a format that LaTeX understands right away, i.e. 0.7\linewidth instead of 70%. And you have to double the backslash in the R code:
---
output:
html_document: default
pdf_document:
keep_tex: yes
---
```{r setup, include=FALSE}
## Modify the output based on the format we're exporting
knitr::opts_hooks$set (out.width = function (options) {
if (knitr::is_latex_output()) {
options$out.width = '0.7\\linewidth'
}
options
})
```
```{r, lec-4-modularity-cost, echo=FALSE, out.width='100%', fig.align='center'}
library(ggplot2)
library(tibble)
plot.data <- tibble (X = seq (0,100), Y = 2 * X)
ggplot(plot.data, aes(x=X, y=Y)) +
geom_path()
```

Related

flutter TexView - Insert values to raw strings

I'm trying to get the flutter package flutter_tex (Link) working.
It renders fine, but I am not able to put my own values into the formula.
Example (factorB is the variables name):
String second = r"<br> $$x={\sqrt{factorB}}$$";
Result:
I also tried the $-operator before the variable like in a dart string, but because a raw string is needed, this doesn't work.
Is there a way to implement variable values in a raw string with this package?
It is not strictly necessary to use a raw string. Doing so just makes it more convenient to type all the back slashs and brackets required in LaTeX code.
You could always work with regular string, this just makes it more incovenient to produce correct LaTeX. Instead, I would suggest manually replacing the placeholder:
int number = 42;
String latex = r"<br> $$x={\sqrt{factorB}}$$";
latex = latex.replaceAll("factorB", number.toString());
If you would like to support more advanced interpolation via expressions,
I would recommend placing all of that logic inside the replace method. This here is an example
int factorB = 17;
int factorB = 1;
String latex = r"exprA(x${exprB}$exprC)²exprD";
latex = latex.replaceAll("exprA", 42);
latex = latex.replaceAll("exprB", factorB >= 0 ? "+" : "");
latex = latex.replaceAll("exprC", factorC >= 0 ? "+" : "");
latex = latex.replaceAll("exprD", 33);

Read data to matlab with for loop

I want to read the data of a file with size about 60 MB into matlab in some variables, but I get errors. This is my code:
clear all ;
clc ;
% Reading Input File
Dataz = importdata('leak0.lis');
%Dataz = load('leak0.lis');
for k = 1:1370
foundPosition = 1 ;
for i=1:size(Dataz,1)
strp = sprintf('I%dz=',k);
fprintf(strp);
findValue = strfind(Dataz{i}, strp) ;
if ~isempty(findValue)
eval_param = strp + '(foundPosition) = sscanf(Dataz{i},''%*c%*c%*f%*c%*c%f'') ;';
disp(eval_param);
% str(foundPosition) = sscanf(Dataz{i},'%*c%*c%*f%*c%*c%f') ;
eval(eval_param);
foundPosition = foundPosition + 1 ;
end
end
end
When I debugged it, I found out that the dataz is empty & so it doesn't proceed to next lines. I replace it with fopen, load & etc, but it didn't work.
From the Matlab help files, import data is likely failing because it doesn't understand your file format.
From the help files
Name and extension of the file to import, specified as a string. If importdata recognizes the file extension, it calls the MATLAB helper function designed to import the associated file format (such as load for MAT-files or xlsread for spreadsheets). Otherwise, importdata interprets the file as a delimited ASCII file.
For ASCII files and spreadsheets, importdata expects to find numeric
data in a rectangular form (that is, like a matrix). Text headers can
appear above or to the left of the numeric data, as follows:
Assuming that your .lis files actually have delimited text.
You should adjust the delimiter in the importdata call so that Matlab can understand your file.
filename = 'myfile01.txt';
delimiterIn = ' ';
headerlinesIn = 1;
A = importdata(filename,delimiterIn,headerlinesIn);

Pygal not rendering Legend properly in PNG

I am trying to render a chart in PNG format, because this image is going to be embed in a email template and SVG are not supported on every email client, when I render the chart in SVG format it looks quite fine but when I render on PNG, the chart is fine but the legend looks wicked
The code I am using is pretty straightforward. I've installed PyCairo tinycss and cssselect
...
pie_chart = pygal.Pie()
pie_chart.title = 'Email usage on %s ' % month_str
for k, v in data.items():
if k in EMAIL_STATUS:
pie_chart.add(k, float(v))
if settings.DEBUG == True:
path = os.path.join(settings.APP_ROOT, 'static')
else:
path = settings.STATIC_ROOT
path = '%s/images/chart.png' % path
pie_chart.render_to_png(path)
Any idea what am I missing here?
Thanks

Reading and Writing Header to File using MATLAB

I am reading in a .txt-file using importdata:
MC_file = importdata('D:\Simon\Dropbox\SGM\Gigerwald\Results\Multi_SGM_10000.txt');
Data = MC_file.data
Header = MC_file.colheaders
which gives me a struct-variable with the header and the data-body:
data = 10000x52 double
colheaders = 1x52 cell
Now i'd like to change the data, i.e. add some columns to the end of the file and then print the file again with the same header + 5 new columns.
The body can be printed easily using dlmwrite(), e.g. like this:
dlmwrite('SGM.txt',Data,'precision',8,'delimiter','\t','newline','pc','-append')
...but how do I print the header into the file, of which I have a array of cells? I've seen this, but since I end up with 57 columns hard-coding is not an option...
The solution is attainable using repmat. Assuming that your file is tab separated, you can use:
NUM_COLS = 57;
A=cellstr(num2str((1:NUM_COLS)'))';
fid=fopen('newfile.txt','w');
fprintf(fid,repmat('%s\t ',[1,NUM_COLS]),A{:});
fclose(fid);
The result can be previewed with:
sprintf(repmat('%s\t ',[1,NUM_COLS]),A{:})

How to use the current date as name for a CSV file

How do I create a csv file with date as the name of the csv file. I tried doing it, but the date won't appear only the name does. The language is Scilab which is similar to Matlab.
I do not understand your question fully. But following the csvWrite documentation and the date documentation. You could do someting like this
filename_with_date_string = date() + ".csv";
directory_path = TMPDIR;
// Some matrix you want to save
M = [1:10] * 0.1;
// Create the file
file = fullfile(directory_path, filename_with_date_string);
// Fill it with your matrix
csvWrite(M, file);