I'm using the Matlab publishing tool Latex to add formula's
I want to show an error function: e.g. erf(x). The problem is that this function is not default in latex. As a result you will get something like "er f(x)".
Now, in Latex you can add this new function using.
\DeclareMathOperator{\erf}{erf}
However, Matlab does not accept this.
Error updating Text.
Character vector must have valid interpreter syntax:
$$\DeclareMathOperator{\erf}{erf}$$
I've been looking around, but found nothing, so help would be appreciated.
Related
I stumpled upon the following issue:
I turned a symbolic expression to his latex form like the following:
>> latex(sym(#(x)airy(x)))
ans =
'\text{airy}\left(0,x\right)'
This is an example of a builtin function with no latex equivalent (unlike sin, cos, ...). Trying to use this expression in a legend using legend({'$\text{airy}...$'}, 'Interpreter', 'latex'), I get the following error:
Character vector must have valid interpreter syntax:
$f(x) = \text{airy}\left(0,x\right)$
This is caused by text being provided by amsmath package which is not loaded by the interpreter somehow. So I can fix this by replacing the text directive with textrm:
replace(latex(sym(f)), '\text{airy}', '\textrm{airy}')
But this shouldn't be needed. I expect a string produced by the latex directive in matlab to be processible by the latex interpreter coming with (or linked to) matlab.
Do you have any idea on how to fix this, e.g. on how to load the amsmath package? Yes, this is maybe possible, but very hard, so I'm not searching for a solution to hacky load it somehow. There seems to be the possibility to add some own stylesheet while publishing, but I couldn't find anything related for legends or figure manipulation, this would be a better approach. You have any ideas on this?
I recently started using Live editor in MATLAB and I inserted a function inside it. But, apparently, I cannot execute that particular section of code where I type function. Even the section break disappears.
Is it that using function is not suitable for live editor?
Apparently the MATLAB parser did not join the 20th century until partway through 2016, and could not interpret function definitions in scripts (live or otherwise) until R2016b. In the web-based docs, there is a notice at Add Functions to Scripts, but it took me a while to find this out because the builtin docs in R2016a or earlier do not explicitly contain this information. It is implied by the tutorials that tell you to create a new file for each function (which to me, a python programmer, sounds more like strange advice than a restriction).
Trying to define a function in a live script gives confusing errors. For example, if you create a cell with this content:
function y = myfunc(x)
y = 2*x;
end
It will underline the keyword function with a popup error that reads:
Parse error at FUNCTION: usage might be invalid MATLAB syntax.
Might be? Whom shall I ask? Upon running the cell, it prints an error after the first line:
All functions in a script must be closed with an end.
I eventually made this discovery myself thanks to a helpful message if the first thing you happen to do in a new empty live script is to start typing function on the first line; as soon as you hit the spacebar a message pops up at the top saying:
Functions and classes are not supported in the Live Editor. To continue, save the file as a plain text code file (.m).
It should work as when you add a function inside a script. For example, like this:
What function are you exactly trying to code?
The publish function in MATLAB works for both scripts and functions,
while the one for Octave works only for scripts: if I enter
publish myFunc.m
Octave gave
error: publish: Only Octave script files can be published.
Am I able to publish a function in Octave? If yes, how?
You can use Octave Forge generate_html package which is meant to generate html of individual functions. It is mostly used to generate the documentation of Octave Forge packages, and its defaults reflect that, but you could add any style you want.
This package will use the same help text that the help function in Octave sees which is the first block of comments in the file that does not start by Copyright or Author. You can have it in plain text but for nicer formatting, you can use Texinfo. In that case, the first line of the help text should be -*- texinfo -*-. There is a page on the Octave wiki with tips on how to write nice help text including a short section on Texinfo syntax (the actual Texinfo manual can be a bit overwhelming).
In addition to the help text, the generate_html package also identifies %!demo blocks and generates a section with the demo code and output it generates, including figures.
The best way to see how help text and demo blocks work in Octave is to check the source (as #Andy pointed out in the comments). For example, see the source for inpolygon (scroll to the bottom to find the %!demo blocks, right before %!test and %!error). The generate_html package generates this page (note the Demonstration blocks).
This is a "multiple questions in one" question, making lots of assumptions in between, so let's address those first:
1. I'll start by the question in the comment, since that's the easiest: Matlab's publisher is not a code documentation tool. It's a "make a quick report that includes both text and code to show at your supervisor meeting or write a quick point in a blog" tool. So the link you point to is simply irrelevant in this case, since that talks about documentation for matlab code.
2. The fact that matlab's publisher also "works for functions", particularly given the first point here, should be considered to be more of a bug than a feature, or at most as a trivial undocumented extension. If you look at the matlab documentation for the publish command, you'll see it expects a filename, not a function with arguments, and the documentation only talks about scripts and makes no mention of 'function' compatibility.
3. Furthermore, even if you did want to use publisher as a "documentation tool", this is counterintuitive for functions in this format, since you need to provide arguments in order for publisher to work (which will not be shown in the actual report), you'll need a modified version that displays intermediate calculations (as opposed to your normal version which presumably does not), and the function just spews an ugly ans= blabla at the end of the report. If your end goal is documentation, it might be best to write a bespoke script for this anyway, showing proper usage and examples, like matlab does in its actual documentation.
Having said all that, yes, there is a 'cheat' you can do to include a function's code in your published report, which is that, in octave (also matlab since R2016b), functions can be defined locally. A .m file that only contains function definitions is interpreted as a function file, but if there are other non-function-declaration instructions preceding the function definitions (other than comments), then it is seen as a script. So if you publish this script, you effectively get a published report with function code in it:
%% Adding function
% This function takes an input and adds 5 to it.
%% Example inputs
In = 10;
%% The function itself
% Marvel at its beauty!
function Out = myfun(In)
%% Here is where the addition takes place.
% It is a beautiful addition
Out = In + 5;
end
%% Example use
Out = myfun(In)
(If you're not happy about having to create a 'wrapper script' manually, you can always create your own wrapper function that does this automatically).
However, both the matlab and octave publishers are limited tools by design. Like I said earlier, it's more of a "quick report to show numbers and plots to your supervisor" tool, rather than a "make nice documentation or professional reports" tool. Instead, I would invest in a nice automated latex workflow, and have a look at code formatting tools for displaying code, and simply wrap that code in a script that produces output to a file that you can then import into latex. It may sound like more work, but it's a lot more flexible and robust in the long term, particularly since the formatting commands can be very quirky as well as limited.
I know of the cprintf Undocumented Matlab way of changing the color and other font properties in the command window but I also saw this symbols in plots. This shows that Matlab supports TeX markup in plots at least. I played with it for a while and found it very useful. So much so that I wanted to find a way to include this in the command window.
I first tried sprintf('\color{red} Something\n') and was rewarded with an error that \c is not a recognized escape sequence. Google was no help either.
This is a way to use the some of the other formatting options in the command window?
The command window doesn't support TeX. Sorry. The TeX support is part of the routines that generate the figures, not the code that displays the command window.
In essence, the command window is, by modern standards, a pretty boring terminal emulator. There's not much you can do with it.
If you're looking for something to do math in that generates fancy notebooks on the fly that combine the commands you type and their results in a nice-to-read, modern way, you might just want to avoid Matlab and have a look at Jupyter (formerly IPython) Notebooks, which of course support MathJax (and thus, LaTeX math syntax): https://try.jupyter.org/
Suppose, I use Emacs to write a function in latex:
\begin{equation}
\label{eq:myeq}
x=2y
\end{equation}
that gives me
x = 2y (1)
in a resulting .dvi. And suppose I have dozens of such equations. Here is the problem: if I have to reference this equation but don't remember its name I want to be able to reference it by it's number ((1) in the shown case).
The story. Actually, I know there is an emacs package that has such a function. Like this: I press key combination, the function asks me to enter an equation number and returns it's name, nicely surrounded by (\ref{ and }). I used it, but after changing OS on my computer I've lost that Emacs installation. And now I don't remember what was the name of that package I once installed. Or maybe it was just a function in .emacs. Googling by key words gives RefTex, but I didn't found that feature in RefTex. So if someone uses that incredible package - please tell me its name!
Found the following package:
http://www.emacswiki.org/emacs/download/AuxLabel
Make sure to read the LIMITATIONS section.