Numbered :math: equations in reStructuredText - python-sphinx

How can I make an equation in restructured text, that is followed by an equation number:
p = f(x) (1)
.. math::
p = f(x)
would only lead to:
p = f(x)

Looking at this a few years later, it appears that the number is still not automatically placed on the right of the equations. I'd therefore like to supplement the accepted answer a bit.
First you add a label to the equation in the rst file:
.. math::
:label: pfx
p = f(x)
This will generate a <span> of class eqno containing the number and link to the equation. To make it show up as you would expect an equation number to show up, you need to add a style to override the default behavior of that class.
I usually do this by adding custom.css to the _static/css folder under my doc root:
.math {
text-align: left;
}
.eqno {
float: right;
}
The math class labels the <div> containing the whole equation. Without the text-align: left; style, all your equations would be centered, making it totally reasonable to number them on the left.
Now you need to register the CSS file in conf.py. I've added the following basic hook:
def setup(app):
app.add_stylesheet('css/custom.css')
Relative paths are resolved from the _static folder. You can add globbing to pick up all the files in the folder at once, but this should suffice.
The reason that #EngineeredBrain's comment reports a number on the previous line is that their equations are too long and don't fit on the same line. I'm sure there is a way to style them to fit no matter what, but I won't even attempt to go into that here.
This will only work in sphinx (not rST), and only with HTML output as far as I'm aware. I'll try with latexpdf one day and update.

.. math:: p=f(x)
:label: eq:pfx

Related

Right-to-left text seems to be displaying out of order

I have the following text:
$3.00 x 2 = $6.00
When I apply direction: rtl; to the body of the page, this text displays as
x 2 = $6.00 $3.00
Can anyone explain why it's displaying this way?
A full answer to your question would have to explain how the Unicode Bidirectional Algorithm works, and it's immensely complicated.
From my limited understanding, the algorithm has detected that "x 2 = $6.00" and "$3.00" are two separate "runs" of text that should be displayed in left-to-right order. As the whole block is right-to-left, you see the two runs in RTL order.
It's not clear if your question is trying to solve a problem, or if you're just curious. However, if you need to display your equation fully LTR, but in the middle of some other RTL text you can use Unicode control characters.
e.g. The text in this RTL block will display the text between the two markers as a continuous run of LTR text.
<body dir="RTL">
‪$3.00 x 2 = $6.00‬
</body>
Simpler in most cases (if you can) is to isolate the LTR text with HTML elements, e.g:
<body dir="RTL">
<span dir="LTR">$3.00 x 2 = $6.00</span>
</body>
As I can't read Arabic or Hebrew I can't tell you how your example text should appear when embedded into RTL script. However, you do have control over the rendering.

How to increase the space between my posts?

I don't know anything about HTML codes and I'm using the 'Galauness Blogger Theme' I found on the internet. I changed some things that weren't too hard and the only thing I currently don't like about my blog is the fact that it seems like the wrong title belongs to the wrong blogpost.
If you visit my website it looks like the second row of text belongs to the first row of images but that isn't the case.
Would anyone mind helping me with a) decreasing the space between the text and the image or b) increasing the first row of images and the second row of text?
My website is www.lemontierres.com
Thank you in advance
Decreasing the space between headline and image is possible, but can get complicated. The reason is each headline in your CSS has a height set to 40px and a padding of 10px. You'll find this in the css for the selector .post h3. If the height:40px; would be removed, it looks better as currently all your headlines only have one line, but in case of a single headline with 2 lines the three entries in each row wouldn't be aligned anymore - the fixed height takes care about keeping everything aligned.
To have more space between the entries, you can add padding for a wrapper element - e.g. every entry is wrapped in a div with the classes post and hentry, so adding
.post.hentry {
padding-bottom: 30px;
}
will add more space between each row.
I've noticed that adding padding to the outermost wrapper - the div with the class date-outer - leads to weird results, but adding it to .post.hentry seems to work.

How to create floating figures in reStructuredText / Sphinx?

I want to have a figure with text wrapped around it.
This is what I'm saying:
Installation of Optional Accessories
====================================
.. warning:: Never plug in or unplug a Hand Robot or a Grasp Sensor while the robot is turned on, as the system will not function properly and damage to the robot could occur.
Installing a Hand Robot
-----------------------
.. _`fig-attach-hand-robot`:
.. figure:: attach-hand-robot.*
:scale: 40%
:align: right
Attach Hand Robot
Make sure the robot is turned off as described in the section :ref:`turn-off-robot`.
Take the hand robot out of the grounded bin that sits on top of the electrical panel (if you have an adjustable height table) or sits on top of the rear table (if you have a fixed height table). Make sure not to touch the pins on the electrical wiring while doing so. Insert the conical protrusion of the hand robot into the conical receptacle (see :ref:`fig-attach-hand-robot`). Once the hand robot is supported by the InMotion Arm Robot, make sure the two knobs below the Hand Robot have engaged and sprung in. If they have not, twist them until they do as shown (see :ref:`fig-knobs-in`).
and this screenshot of PDF output is what I'm getting.
Why is the figure caption centered, rather than under the image?
Why isn't the body text ("Make sure ..." and "Take the ...") on the LEFT of the image, rather than underneath it? I want the figure to float right and have the text on its left.
I have found that figures float to the side with :figwidth: and :align: specified. (Using the readthedocs theme.)
.. figure:: images/myimage.jpg
:figwidth: 40%
:align: right
https://docutils.sourceforge.io/docs/ref/rst/directives.html#figure
So, I did some research into reStructuredText and it seems what you want is not actually possible.
The documentation for the figure and the image directives never mention the ability to wrap text around the object.
This might be a feature request to provide to the Sphinx developers although I suspect they'll reject it because it isn't explicitly mentioned in the rst specification.
I was hoping the bounty would garner this some attention but I suspect is hasn't.
Though it is too late but maybe the answer would help future people.
You can use the sidebar directive to put the image.
.. sidebar:: mandatory_title. Use can use image caption here
.. Figure:: 1.png
In order to deal with images as they were part of the text you may actually use substitutions.
Here an extract from the documentation that can be helpful:
The |biohazard| symbol must be used on containers used to
dispose of medical waste.
.. |biohazard| image:: biohazard.png
I hope this helps
If anyone else runs into this problem then this bit of code might be a help. I decided that I didn't want to hack the actual sphinx code so I made a very short python script applied to the generated _build/latex/pi3d_book.tex to convert the \includegraphics that had \hfill before or after into wrapped images. There will be lots of things that stop this working such as putting images inside lists or scaling images. The sphinx directives in my rst are like
.. image:: perspective.png
:align: right
You obviously have to change the file names and paths to suit your setup. From my spinx project I run
$ make latexpdf
$ python wrapfix.py # or whatever you call this file
program listing of wrapfix.py
import subprocess
with open("_build/latex/pi3d_book.tex", "r") as f:
tx = f.read().splitlines()
txnew = []
flg1 = True
for line in tx:
if line == "" and flg1:
txnew += ["\\usepackage{wrapfig}",""]
flg1 = False # just do this once before first blank line
elif "includegraphics{" in line and "hfill" in line:
fname = line.split("{")[2].split("}")[0]
if line.startswith("{\\hfill"): # i.e. right justify
fl_type = "R"
else:
fl_type = "L"
txnew += ["\\begin{wrapfigure}{" + fl_type + "}{0.35\\textwidth}",
"\\includegraphics[width = 0.3\\textwidth]{" + fname + "}",
"\\end{wrapfigure}"]
else:
txnew += [line]
txnew = "\n".join(txnew)
with open("_build/latex/pi3d_book.tex", "w") as fo:
fo.write(txnew)
subprocess.Popen(["pdflatex", "pi3d_book"], cwd="/home/jill/pi3d_book/_build/latex")

Block elements overlapping with singularitygs

I'm doing my first project with singularity grid system and I'm loving it so far. However, I'm having a strange problem in a section where I have an <h2> and <h3> elements overlapping... really having an hard time figuring what's the problem.
My project in development is available at:
http://senseslabv3.brunomonteiro.mixture.io/
First <section> with class=intro.
Does anyone have a clue about it's going on?
Thanks for your time.
As the others have said, you need to clear your floats. By default, Singularity's output style is "Isolation" which requires a knowledge of how floats should get cleared (clear: left, clear: right, clear: both, clear: none). Singularity assumes no clear (clear: none) which means that grid items may overlap if not properly cleared. It does this to adhere to the most common mental model for the Isolation output method, specifically placing blocks at a discrete point on the grid. Clearing your floats will clear them to an item's margin edge, most visibly by creating new rows. See the Mozilla Developer Network article on Clear.
Note, clearing your floats and clearfixing as proposed by lolmaus actually do different things. Clearing your float will clear items to margin edges, whereas clearfixing an item will ensure that all of its floated children are properly contained.
The Float output adheres to a different mental model, one of walking across a row of your grid, and therefore automatically clears your floats for you. If you'd prefer to use the Float output style as your default, simply add $output: 'float' to your Sass file before calling your grid. This will change your global output style context. Alternatively, you can use float-span to use the Float output style mental model and output on-demand instead of grid-span, or pass $output-style: 'float' as an option to grid-span.
Take a look at the documentation for Output Styles, Output Span, Float Span, and Context Overrides in grid-span for a deeper dive into the different output styles and options available in Singularity.
Clear both needs to be declared somewhere below your grid-span mixin .tag h3 {clear: both;}
instead of the ugly <div style="clear: both;"></div> consider this:
.intro h2 {
#include pie-clearfix; }
Or, if you use toolkit:
.intro h2 {
#extend %clearfix-micro; }
We might better address your problem if you share your SASS code.
This is an old question but I just ran into the problem. Snugug's answer worked perfect but I wanted to show the code that worked for me. (Couldn't put code in a comment)
//Main content container
.l-main {
#include breakpoint(80em) {
#include grid-span(16, 3, 20);
}
}
// A full width banner inside content container. I needed this to clear because there are several other smaller columns/grids above and below the banner.
.b-banner {
#include breakpoint(80em) {
#include float-span(16, 1, last);
}
}

Google Code Prettify - stripes/piano keys without line numbers?

I'm using Prettify (from Google Code - https://code.google.com/p/google-code-prettify/). When one adds the linenums attribute then it renders the program using alternating background colors on each line (i.e., it uses "stripes" or "piano keys"). When one removes that attribute Prettify no longer stripes the lines.
Is it possible to get Prettify to stripe source code WITHOUT also adding line numbers?
If you look at the themes gallery you'll see that this is affected by the stylesheet, and that there are some with line numbers on every line.
Something like
<style>li.L4, li.L9 { list-style-type: none }</style>
should do it.
The operative part of the default stylesheet is
li.L0,
li.L1,
li.L2,
li.L3,
li.L5,
li.L6,
li.L7,
li.L8 { list-style-type: none }
which turns off list bulleting for all items with index i where (i % 10) ∉ (4,9), hence the li.L4, li.L9 selector fills the gaps.
Answer is yes. Mike Samuel's answer seems to misinterpret or ignore the question.
The "piano keys" striping of code (not line numbers) occurs with every other line of text having an alternating background color whenever line numbers are requested. I wanted to turn it off when showing line numbers, but the original questioner wanted to know how to turn it on (like for an Excel spreadsheet) but without showing line numbers. Neither question, however, would seem to have anything to do with the line numbers or the li style directly, hence the confusion.
To fix this, a background-color property can be added; this overrides the striping colors of the numbered prettify commands and allows custom stripes.
An answer to both my problem and the original was eventually found. A style was added to override the current prettify code such as the following. You must still specify linenums in the prettify class but none will be shown:
highlight every 5th line no nums:
<style>
li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8
{ background:#000022 !important; list-style-type:none !important}
li.L4,li.L9
{ background:#080833 !important; list-style-type:none !important}
</style>
highlight every 5th line with nums:
<style>
li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8
{ background:#000022 !important; list-style-type:decimal !important}
li.L4,li.L9
{ background:#080833 !important; list-style-type:decimal !important}
</style>
Important: there is no comma before the curly bracket. If you insert such
a comma the style will fail!

Resources