MathJax error on second time render - mathjax

I render dynamically the following code using MathJax:
When $a \ne 0$, there are two solutions to \(ax^2 + bx + c = 0\) and they are
$$x = {-b \pm \sqrt{b^2-4ac} \over 2a}.$$
In equation \eqref{eq:sample}, we find the value of an interesting integral:
\begin{equation}
\int_0^\infty \frac{x^3}{e^x-1}\,dx = \frac{\pi^4}{15}
\label{eq:sample}
\end{equation}
using Hub.Queue
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
The first time the rendering works well. But when I try to render again using the same code only the first part is rendered, but the second part (which starts from \begin) does not parsed and shown in the black frame. Why it happens?

The problem is the use of \label. When the math is re-typeset, the label already exists (from the first typesetting), and so the TeX input jax reports that as an error (disable the noErrors extension to see the error message).
You need to reset the labels (and presumably the equation numbers) before you typeset again. To do that, use
MathJax.Hub.Queue(
["resetEquationNumbers",MathJax.InputJax.TeX],
["Typeset",MathJax.Hub]
);
instead of the MathJax.Hub.Queue(["Typeset",MathJax.Hub]) call.

Related

noUISlider - Any way to place labels within the connect segments

I have a noUiSlider with several handles to allow specifying several contiguous date periods (example = Feb to Apr, May to July, and Aug to Sept). Ideally I would like to have labels that appear centered on the connect divisions to describe what each period relates to (ex. "Current Period", "Next Period"). I was thinking I could do this by setting a centered background image on the noUi-connect divisions.
However, the noUi-connect divisions use transform (translate/scale) styling which results in my background images being scaled which I do not want.
I also thought maybe I could revise the javascript to generate an outer division around each nonUi-connect division, and I would apply the background onto the outer division instead - but I was unable to get the background from the outer division to appear.
Any other ways I could accomplish this? The only other thing I can think of it to have floating divisions defined outside of the noUiSlider object which I would need to reposition whenever I detect changes in the handle positions.
You can add an element outside of the connects and absolutely position it.
A quick version for a slider with two handles (showing the value for the first handle):
var origin = slider.querySelector('.noUi-connects');
var node = document.createElement('div');
node.style.textAlign = 'center';
node.style.position = 'absolute';
node.style.zIndex = '10';
node.style.fontSize = '10px';
origin.appendChild(node);
slider.noUiSlider.on('update', function(values, handle, unencoded, tap, positions) {
node.style.left = positions[0] + '%';
node.style.right = (100 - positions[1]) + '%';
node.innerText = values[0];
});
Just realized another approach is to set the innerHtml of the specific noUi-connect divisions to my label values. Simpler than playing with background images.
But the transform styling still affects the labels, so the end result is not better. Maybe I can load the innerHtml with an inner division that somehow ignores the transform settings but I haven't figure out how to do that yet. transform: none does not make any difference.

How to delete multiple plot axes made with subplot on scilab

how are you. I've all day trying to do the following: I'm designing a PID controller and depending on how much data I want to analize then I show only one plot or four plots in the same figure (this last multiple plot is made with subplot).
All the calculation and plots are made in one function with options in function dependind on what I want to see, I have several buttons and text inputs in the application that I'm coding and right know the PID design and the simple or multiple plots are working; my current program is that the axes handle is being assigned to local variables inside the function and of course this variables are distroyed when the program exits the function.
A more detailed explanation of the problem is the following: if I select multiple plot and then I want one plot then I don't know how to delete the four axes to clean the figure without closing it, so when I want to activate the simple plot then the program only erases one of the four plots and prints the simple plot over the remaining three while the remaining three subplots remains visible. Following is the piece of code that is giving me problems:
grafico=sistem_graf;
disp(grafico);
if(isdef('aa')==%T);
scf(aa);
delete(get("current_axes"));
disp("aa");
end
if(isdef('bb')==%T);
scf(bb);
delete(get("current_axes"));
disp("bb");
end
if(isdef('cc')==%T);
scf(cc);
delete(get("current_axes"));
disp("cc");
end
if(isdef('dd')==%T);
scf(dd);
delete(get("current_axes"));
disp("dd");
end
select ventana
case 0
tipoG=0;
plot2d(T,graffta);
xtitle('Ω(S)/R(S): Respuesta al escalón (Sistema original)','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds = [1/3 0 2/3 1];
case 1
tipoG=1;
// delete(get("current_axes"));
plot(T,[graffta;sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Ω(S)/R(S): Respuesta al escalón','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds=[1/3 0 2/3 1];
case 2
tipoG=2;
//delete(get("current_axes"));
subplot(421);
plot(T,[graffta;sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Ω(S)/R(S): Respuesta al escalón','t [s]','Vel [rpm]');
legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
aa=get("current_axes");
aa.axes_bounds=[1/3,0,1/3,1/2]
subplot(422);
plot2d(T,(kt*kp/(j*la))*(1/(afta*bfta))*(1+(1/(afta-bfta))*(bfta*exp(-afta*T)-afta*exp(-bfta*T))));
xtitle('Ω(t)','t [s]','Vel [rpm]');
bb=get("current_axes");
bb.axes_bounds=[2/3,0,1/3,1/2];
subplot(423);
plot2d(T,(kt/(j*la))*(1/(afta*bfta))*((1/(afta-bfta))*(bfta*afta*exp(-bfta*T)-afta*bfta*exp(-afta*T))));
xtitle('Variación de velocidad angular dΩ/dt','t [s]','Ac. [rpm/s^2]');
cc=get("current_axes");
cc.axes_bounds=[1/3,1/2,1/3,1/2];
subplot(424);
plot(T,[sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
xtitle('Controladores','t [s]','Vel [rpm]');
legend(['C/control P';'C/Control PI';'C/Control PID']);
dd=get("current_axes");
dd.axes_bounds=[2/3,1/2,1/3,1/2];
end
the four if(isdef("variable_name")) never doesn't works because when the program enters the functions those variables doesn't exists yet, those are created after the plots with subplot.
If the graphics properties are displayed from the Edit menú of the figure then all the children axes are shown but if I try to get the axes with for example
a=get(sistem_graf.Axes(1))
then a console display is shown saying that the property "Axes" doesn't exist so I don't know how to solve the problem.
Please help!
Thanks in advance.
Update 03/06/2021:
I found a way to have the behavior I need an is working at 90%, it only fails when I go from detailed plot to simple plot (is erasing one graphic) but backwards it works fine, I changed all the code before "Select ventana" to the following
if(imps(1) ~= -1)
if(size(imps) == 1)
sca(imps(1));
disp(imps(1));
delete(get("current_axes"));
elseif(size(imps) > 1)
sca(imps(1));
delete(get("current_axes"));
sca(imps(2));
delete(get("current_axes"));
sca(imps(3));
delete(get("current_axes"));
sca(imps(4));
delete(get("current_axes"));
end
end
and filled an array whose content is all the axes ploted last time that the function was called, that array is returned from the function to a global variable and finally that global variable is feed to the function in the next call and assigned to the local variable "imps".
I think is some kind of crazy but apparently scilab doesn't have incorpotated the static variable aproach.
If I have understood your problem, the small example below should help you:
function fun(nb)
ch = gcf().Children;
delete(ch(ch.type=="Axes"))
t = linspace(0,1,100);
if nb==1
plot(t,t)
else
for i=1:nb
subplot(2,2,i)
plot(t,t.^i)
end
end
endfunction
clf
uicontrol("style","pushbutton","units","normalized","Position",[0.1 0.1 0.4 0.1],"string","one plot","callback","fun(1)");
uicontrol("style","pushbutton","units","normalized","Position",[0.5 0.1 0.4 0.1],"string","four plots","callback","fun(4)");
I think that the mechanism you were missing is the following:
ch = gcf().Children;
delete(ch(ch.type=="Axes"))
i.e. something that allows to delete the plot axes and keep the uicontrols alive. The above construct uses logical indexing. Here ch.type=="Axes" is a vector of boolean values with components i egal to true when the type field of corresponding component ch(i) is Axes. Then, ch(ch.type=="Axes") is the subset of components of ch such that the boolean index has value true. Hence, at last, the subset of components of ch which are of Axes type.

Bumpy jigsaw type data into curve data

I have a problem I solved in Excel but I am completely stuck in Matlab.
A weight measuring how much liquid I pump is refilled when its almost empty. Here is a picture
Now I want to see it in one motion, not like a jigsaw. Here is my solution, doing it manually in Excel:
Now to Matlab where this should be done automatically: I know how to index the row before and after I have the bumps, but I'm kind of stuck now. I do d=diff(x) ok and now I can replace the high peaks when the bumps occur (i=d(:,1)>0) with 0 so it never happened. And how do I translate it back? Somehow "undiff(x)"? im completely lost.
Here's an extract of my x:
2533,30
3540,00
3484,90
3430,00
3375,00
3320,20
3265,60
3210,60
3155,80
3101,20
3046,50
2991,70
2937,00
2882,50
2828,10
2773,80
2719,30
2664,90
2610,50
2556,10
2501,60
3508,00
3454,00
3399,70
3352,10
Like this?
temp = [0; diff(x)];
temp(temp < 0) = 0;
y = x - cumsum(temp);
y(temp > 0) = interp1(y, find(temp > 0) + 0.5);
plot(y);
hen using the default plot() function, matlab will automatically draw a line plot and connect each point in the data you are plotting.
Instead it seems like you just need to plot the points individually and unconnected. So if you are currently doing something like plot(x) or area(x) instead try plot(x,'o'). This will cause Matlab to plot the points individually without connecting them as lines.
If you'd like to change the marker type used to plot each point, use doc linespec to get more info.

how to use an atomic vector as a string for a graph title in R

I'm trying to plot a graph from a matrix of z-scores in R, i would like to build a function to iterate through each column using the column header as part of the title and saving each graph as a png.I think I know how to do the iteration and saving graphs as pngs but I am getting stuck with using the vector as a string. I tried to upload the matrix with no column headers and then store matrix[1,] as a variable 'headers' to use. Then I tried to plot:
plot(1:30, rnorm(30), ylim=c(-10,10), yaxs="i", xlab = "Region", ylab = "Z-Score",main = "CNV plot of " + headers[i], type = "n")
I get:
Warning message:
In Ops.factor(left, right) : + not meaningful for factors
I try without the '+' and it says:
Error: unexpected symbol in ...
So then I looked around and found 'paste(headers[i],collapse=" ") which I though I could substitute in but it just puts the number '28' as the title.
I've tried what I thought was another potential solution:
plot(1:30, rnorm(30), ylim=c(-10,10), yaxs="i", xlab = "Region", ylab = "Z-Score",main = "Z-scores of " $headers[i], type = "n")
and I get:
Error in "Z-scores of "$headers :
$ operator is invalid for atomic vectors
I'm new to R and this seems like something that would be so simple if I happened to stumble across the right guide/tutorial after hours of google searching but I really don't have that kind of time on my hands. Any suggestions, pointers or solutions would be great??
If you want to insert values from variables into strings for a plot title, bquote is the way to go:
headers <- c(28, 14, 7) # an examle
i <- 1
plot(1:30, rnorm(30), ylim=c(-10,10), yaxs="i",
xlab = "Region", ylab = "Z-Score", type = "n",
main = bquote("CNV plot of" ~ .(headers[i])) )
Have a look at the help page of ?bquote for further information.
paste("CNV plot of", headers[i]), should work. You only need collapse if you are pasting vectors of length greater than one (headers[i] should be one length, even if header is not). R doesn't have any concatenation operators, unlike PHP, JS, etc (so +, &, . will not work, you have to use paste).
Note that your paste was paste(headers[i],collapse=" "), and if that just plotted 28, it suggests your headers vector doesn't contain what you think it does (if you didn't want 28 to be displayed, that is.
Try just looping through your vector and printing the paste command to screen to see what it displays (and also, just print the vector).

Raphael - Transform after Serialization

I am using Raphael to draw some paths. Each path has an associated rectangle [container] the size and position of the bounding box. I am using the container for dragging both shapes.
In the move callback, I update the both positions so they both move together.
This all works great until I serialize. I am only serializing the path, then creating the container on the fly after deserialization.
Immediately after converting to json and back, things look fine. I can print out the current transform of the path and it looks correct. Doing any transform on the path after this results in the path being reset and moved to 0,0.
Here is a fiddle that shows the problem.
If you move the rect, you can see both objects move together.
If you click 'Save/Load', things look fine, and the path prints the same.
If you now drag, the path gets reset to 0,0. Printing shows the transform has been reset from 0,0.
I am trying to find out how to make the path move as it did before serialization. Is something getting lost in the process? Or is there an internal state that needs to be updated?
Raphael.JSON serialises data stored in the elements. It does not preserve temporary data stored in the paper object so something does indeed get lost in the process when calling R.clear(). For example drag events bound to elements are not preserved.
However the main issue here is with your drag function, notice how dragging the square a second time applies the transformation from the top left of the paper. I suggest using Raphael.FreeTransform (which you already included in the Fiddle) to handle this.
I wrote both Raphael.JSON and Raphael.FreeTransform plugins and have struggled with the same issues. I'm currently working on an application that lets you save save and restore the state of the paper (similar to what you're doing) and it works fine. If you need any help feel free to open an issue on Github.
You need to capture the initial transform offsets of your elements when the drag starts and use those as the basis for your drag-move transforms. Consider the following:
var start_x, start_y;
cont.drag(function(x, y, e)
{
p.transform('t' + ( start_x + x ) + ',' + ( start_y + y ) );
cont.transform('t' + ( start_x + x ) + ',' + ( start_y + y ) );
},
function( x, y )
{
var start_bbox = p.getBBox();
start_x = start_bbox.x;
start_y = start_bbox.y;
console.log("Drag start at %s,%s", start_x, start_y );
} );
I've staged this in a fiddle located here.
Unfortunately, there is still an issue with the path -- it's offset is being incremented by the difference between it's bounding box y value and the y axis (a difference of 12, to be precise) each time drag is used. I'm not sure where that's coming from exactly.

Resources