How to allocate the label center of case using sphinx? - python-sphinx

I'm writing equations using sphinx. My code looks like this:
.. math::
:label: eq1
\begin{eqnarray}
\begin{cases}
a_{11}x_{1} + \dots + a_{1m}x_{m} = b_1\\
a_{21}x_{1} + \dots + a_{2m}x_{m} = b_2
\end{cases}
\end{eqnarray}
This displays the label (1) on the right side of the first line, but I would to allocate it the center of this {cases}, namely, between the first and the second line.
How do I do this?

This question seems to pertain principally about MathJax usage.
By some experimenting I discovered it understands \smash and that using it provides the hoped for label placement. But perhaps there is some MathJax setting which would avoid using this \smash. Besides \smash works fine only for two or three "cases".
I have moved the \smash approach to second part, where also its drawbacks are explained. Indeed, I have since found a half-satisfying CSS approach.
At any rate, don't use eqnarray there.
CSS based approach
Create a file _static/custom.css in your source repertory with these contents.
.MathJax_Display {
transform: translate(0%,-50%) translate(0%,8px);
}
div.math {
transform: translate(0%,50%);
}
Add this at end of conf.py:
if html_theme != 'alabaster':
def setup(app):
app.add_stylesheet('custom.css')
For example I obtain this with 'classic' theme:
This works also with 'agogo' and 'alabaster' themes, but some fine tuning might be needed for equations with only one line to be positioned like the label.
In the case RTD theme it does not work at all.
Support may be browser dependent.
\smash approach
Text before.
.. math::
:label: eq1
\smash{\begin{cases}
a_{11}x_{1} + \dots + a_{1m}x_{m} &= b_1\\
a_{21}x_{1} + \dots + a_{2m}x_{m} &= b_2
\end{cases}}
Text after.
I get from make html (with classic theme)
conf.py contains
extensions = ['sphinx.ext.mathjax',
]
html_theme = 'classic'
By the way your eqnarray is not good mark-up. IF you use it you should use :nowrap: option. See Sphinx doc.
Note Of course usage of cases here is dubious because your example is one of equations. I added &'s but the spacing is one appropriate for cases, which is surely not the one expected. So remove them.
Here is mark-up which does not abuse cases environment:
Text before.
.. math::
:label: eq1
\smash{\left\{\begin{aligned}
a_{11}x_{1} + \dots + a_{1m}x_{m} &= b_1\\
a_{21}x_{1} + \dots + a_{2m}x_{m} &= b_2
\end{aligned}\right.}
Text after.
CAVEAT The smash trick only works with two (perhaps three) equations, even in MathJax.

Related

Writing Macro in ImageJ to open, change color, adjust brightness and resave microscope images

I'm trying to write a code in Image J that will:
Open all images in separate windows that contains "488" within a folder
Use look up tables to convert images to green and RGB color From ImageJ, the commands are: run("Green"); and run("RGB Color");
Adjust the brightness and contrast with defined values for Min and Max (same values for each image).
I know that the code for that is:
//run("Brightness/Contrast..."); setMinAndMax(value min, value max); run("Apply LUT");
Save each image in the same, original folder , in Tiff and with the same name but finishing with "processed".
I have no experience with Java and am very bad with coding. I tried to piece something together using code I found on stackoverflow and on the ImageJ website, but kept getting error codes. Any help is much appreciated!
I don't know if you still need it, but here is an example.
output_dir = "C:/Users/test/"
input_dir = "C:/Users/test/"
list = getFileList(input_dir);
listlength = list.length;
setBatchMode(true);
for (z = 0; z < listlength; z++){
if(endsWith(list[z], 'tif')==true ){
if(list[z].contains("488")){
title = list[z];
end = lengthOf(title)-4;
out_path = output_dir + substring(title,0,end) + "_processed.tif";
open(input_dir + title);
//add all the functions you want
run("Brightness/Contrast...");
setMinAndMax(1, 15);
run("Apply LUT");
saveAs("tif", "" + out_path + "");
close();
};
run("Close All");
}
}
setBatchMode(false);
I think it contains all the things you need. It opens all the images (in specific folder) that ends with tif and contains 488. I didn't completely understand what you want to do with each photo, so I just added your functions. But you probably won't have problems with adding more/different since you can get them with macro recorder.
And the code is written to open tif files. If you have tiff just be cerful that you change that and also change -4 to -5.

How do I use multiple fonts ie a composite font in HexaPDF

Our users are giving us Emoji and a lot of other weird characters and the built-in Helvetica can't handle it. Neither can Google's Noto fonts by themselves - I need to figure out how to declare the Noto Font Family in HexaPDF and I can't figure out how to do that with the given documentation. OpenSans was an improvement, but I still want more glyph coverage than that.
Update:
I used this method to set the font:
def self.pdf_summary_font
##pdf_summary_font ||= File.open(Rails.root.join('public',
'OpenSansEmoji.ttf'), 'r')
end
canvas = page.canvas(type: :overlay)
canvas.font(self.class.pdf_summary_font, size: 10)
However, no Noto font ever worked with this - I would get errors like "Missing glyph - 'A'"
The best I could do was to use OpenSansEmoji, and replace missing glyphs with the following block:
begin
style = HexaPDF::Layout::Style.new(font: canvas.font, fill_color: color, stroke_color: color, align: :left, valign: :center)
fragment = HexaPDF::Layout::TextFragment.create(str, style)
layouter = HexaPDF::Layout::TextLayouter.new(style)
layouter.fit([fragment], w, h).draw(canvas, x1, y2)
rescue HexaPDF::Error => e
if e.message.include?('Glyph for')
glyph = e.message.match(/\{(.*?)\}/).captures.first
str = str.grapheme_clusters.map do |char|
if char.dump.include?(glyph)
"\u{FFFD}"
else
char
end
end.join
retry
end
Have a look at https://hexapdf.gettalong.org/documentation/reference/api/HexaPDF/index.html and the configuration option "font_map". This allows you to declare any TrueType file and use it.
You could also use the path to the font file directly with the Canvas#font method.
If you need to cover a wide array of characters you need to use a single font that covers all of them, one of the fonts included in this ZIP file should probably work (Google says 582 languages, 237 regions included).

Dividing by half in ruby to create an effective calculator

For the past while I've been working on a calculator, but have run into problems when needing to divide by a half. I'll add the offending bit of code along with a loop to keep it open below.
on = true
while on == true do
half = 1.0 / 2.0
puts ("FUNCTION IN TESTING MODE, DO NOT EXPECT IT TO FUNCTION PROPERLY")
puts ("Area of a triangle")
print("What is the legnth of the base? ").to_i
base = gets.chomp("base")
print("\nWhat is the height? ")
height = gets.chomp("height").to_i
PreAreaT = base * height
AreaT = PreAreaT * half
puts("The area of the triangle is #{AreaT}")
end
So essentially, how on Earth do I get the program to display an answer, rather than outputting nothing for the answer?
EDIT:As it would turn out the code above is improperly done. I've spent nearly two weeks asking myself why it wouldn't work only to find I had .to_i after a print statement rather than the input.
Your to_i call is switched around here.
print("What is the legnth of the base? ").to_i
base = gets.chomp("base")
Should be the other way 'round.
print("What is the length of the base? ")
base = gets.chomp("base").to_i
Further, chomp will attempt to remove any occurrences of base or height from the string. Be sure that you're intention is to remove those occurrences; if you want to remove whitespace, you'll have to take a different approach.

Wondering what the SCSS equivalent of LESS's escape is?

I'm converting a stylesheet from LESS to SCSS and for the most part it's gone smoothly but there's one issue: I can't seem to get one of my mixins to work.
Basically I haven't entirely jumped on the html5 bandwagon and I still use images for gradients for browser compatibility.
I wrote a PHP script to generate the gradients on the fly (yes, and cache them!) and then LESS takes care of linking to that when the mixin is called.
Anyway,
My old mixin (from LESS):
.verticalGradient(#startColor, #endColor, #height){
#tmpStartColor: escape("#{startColor}");
#tmpEndColor: escape("#{endColor}");
background: #endColor url('#{img_path}gradient/v/5px/#{height}/#{tmpStartColor}/#{tmpEndColor}.png') 0 0 repeat-x;
}
This is what I have so far for my new mixin (SCSS):
#mixin verticalGradient($startColor, $endColor, $height){
background: $endColor url('#{$img_path}gradient/v/5px/' + $height + '/' + $startColor + '/' + $endColor + '.png') 0 0 repeat-x;
}
So here's the issue:
Without being escaped, the url ends up being something like:
/img/gradient/v/5px/25px/transparent/#ffe4c4.png
It should be:
/img/gradient/v/5px/25px/transparent/%23ffe4c4.png
Of course, because of the hash tag, the server can only see up to transparent/ so it doesn't get the color information.
Stripping hash tags IS an acceptable solution if it can be done (although I'd prefer to just escape them as I was doing before), but I can't seem to find any way to do it.
Thanks!
I figured this out.
This got me pointed in the right direction:
How do I load extensions to the Sass::Script::Functions module?
And this got me the rest of the way there:
http://sass-lang.com/documentation/Sass/Script/Functions.html#adding_custom_functions
And here's my code if this is useful to anyone else :)
config.rb (require lib):
require File.dirname(__FILE__) + "/lib/urlencode.rb"
lib/urlencode.rb:
module Sass::Script::Functions
def urlencode(string)
Sass::Script::String.new(ERB::Util.url_encode(string));
end
declare :urlencode, :args => [:string]
end
Then it was just a matter of implementing in my stylesheet:
#mixin verticalGradient($startColor, $endColor, $height){
background: $endColor url('#{$img_path}gradient/v/5px/' + $height + '/' + urlencode($startColor) + '/' + urlencode($endColor) + '.png') 0 0 repeat-x;
}

XNA 4.0 InvalidOperationException was unhandeled

I am using this tutorial to learn a little XNA, and i keep running into problems. I've had to convert alot of the code, since it seems the tutorial do not use XNA 4.0.
But lets cut to the chase!
float aXPosition = (float)(-mCarWidth / 2 + mCarPosition.X + aMove * Math.Cos(mCarRotation));
float aYPosition = (float)(-mCarHeight / 2 + mCarPosition.Y + aMove * Math.Sin(mCarRotation));
Texture2D aCollisionCheck = CreateCollisionTexture(aXPosition, aYPosition);
//Bruke GetData til å fylle en array med fargen på pixlene ved collisons texturen
int aPixels = mCarWidth * mCarHeight;
Color[] myColors = new Color[aPixels];
aCollisionCheck.GetData<Color>(0, new Rectangle((int)(aCollisionCheck.Width / 2 - mCarWidth / 2),
(int)(aCollisionCheck.Height / 2 - mCarHeight / 2), mCarWidth, mCarHeight), myColors, 0, aPixels);
The error i get when i try to debug the code says: InvalidOperationException was unhandeled, The render Target must not be set on the device when it is used as a texture.
I have no clue what to do.
This basically means exactly what it says.
You have to unset the render target from the device by calling GraphicsDevice.SetRenderTarget(null) (or setting it to a different render target). Because you can't use it as both a source texture and a destination buffer at the same time.
Keep in mind that, in this version of XNA, there is no ResolveRenderTarget. Render targets simply are textures.
Note that the tutorial that you are using is pretty terrible. Reading back from a render target like this is extremely slow. Especially seeing as the operations that it is using the render target for (selecting pixels in a transformed region) could easily be done efficiently on the CPU. Consider using this better, official example.

Resources