Repeat or reference a figure in another rst file - python-sphinx

I have two files named a.rst and b.rst, both of which contain a good deal of text. In a.rst, I define a figure:
.. figure:: ../images/some-image.png
:scale: 70%
:align: center
:alt: Some Text
Some Caption
I would like to have the same image and caption in b.rst with the same figure number, But repeating the above code gives me a new figure.
As a compromise, I can refer to this image in b.rst using the :numref: directive, but that does not resolve to the figure. It only displays the name as a piece of code.
I understand that these are two question, but I think they are sufficiently related. How can I repeat or reference a figure defined in a rst file in another file.
Edit to elaborate on the expected output:
I want the resulting files to have the following content:
a.html:
Fig. 1 + caption
Fig. 2 + caption
Fig. 3 + caption
b.html:
Fig. 4 + caption
Fig. 2 + caption
Fig. 5 + caption
Effectively, this would add the figure to b.rst not as a separate entity, but merely as a mirror of what was in a.rst. This is similar to what was discussed here.

Assuming you want to repeat the exact same content in two different files, put that content into a separate file, then include that file wherever you want it to appear.
includeme.rst
Note document root relative path.
.. figure:: /images/some-image.png
:scale: 70%
:align: center
:alt: Some Text
Some Caption
a.rst and b.rst
Below this paragraph should appear an image.
..include:: /includeme.rst
EDIT
To remove the figure number, set numfig to False. This will avoid the incongruent figure numbering, but won't solve it. I think that's the best you can achieve, as Sphinx automatically numbers figures (and other objects) otherwise.

Related

How to make two rows of words as big as one word in InDesign?

Im not sure how to express it so I posted a picture in link below.
It should look like this
Just enter the text on 3 lines like so:
MORE
AT
THE HALL
Then adjust the point sizes, leading, kearning, etc. to create the aesthetic you want.
In this case line 1 and 3 could have full justification.
You can use scaling of the text(as shown in the character panel in attached snapshot) because changing font size also moves the baseline and causes the text to shift downward.
These attributes are also exposed via scripting.

How to convert image to table

I have an image of a table (in my case .gif) and want to extract the table it was (ideally, .ods).
Is there any way to do so? (doing it manually is discarted, since the table has more than 1000 rows and 6 columns)
Here is a part of the image / table:
You will be able to get most of it through OCR, but you'll need to manually verify the data and fix some inaccuracies that will be there. It definitely won't be perfect.
First thing to do is to ensure you have a good quality image for the OCR software:
Here's what I did with your sample png (I'm using Windows):
I opened the image in The Gimp.
Removed the orange/blue backgrounds:
a) Select -> By Color and clicked the blue background
b) I held down Shift and clicked the orange background (this will add it to the current selection)
c) Edit -> Fill With BG Color (this sets it to white)
d) Ctrl-Shift-A to cancel the selection
I removed the partially cut off '305' line:
a) used the Rectangular Select tool button from the palette, and filled the selection with BG Color, as above
Let's remove the table border:
a) Click the 'Fuzzy Select' tool button from the palette
b) Click somewhere on the table border (you should see the 'marching ants' instead of the border)
c) Edit -> Fill With BG Color
d) Ctrl-Shift-A to cancel the selection again
We need to increase the number of pixels that the numbers use so that the OCR can better detect their shapes
a) Image -> Scale Image. I chose to scale by 1000% with Linear Interpolation (the other interpolations won't work as well)
Download and install Tesseract from GitHub
a) At the command prompt type (include the double-quotes to cope with spaces within the path, & change your paths as necessary):
"D:\Program Files (x86)\Tesseract-OCR\tesseract" "d:\temp\your_image.png" "d:\temp\your_txt_file_output"
The output with be a text file with an appended .txt extension. It will still have a few artifacts but we can easily correct those in Notepad++ (or similar):
a) The commas were seen as full-stops, so I did a Find and Replace of "." with "," (I'm assuming you don't have any decimal points in the data!)
b) There were some spaces before a few commas, so I did Find and Replace " ," with "," (note I included a space before the comma in the Find)
c) There were still some spaces in the numbers, so I did a Find and Replace of " " with "" (a space with an empty replace)
This gave the following result:
298 299 300 301 302 303 304
910,820,000 920,820,000 930,820,000 941,820,000
952,820,000 983,820,000 9?4,820,000 210,000
220,000 220,000 220,000 220,000 220,000
220,000 2,500 2,500 3,000 3,000
3,000 3,000 3,000 19,000 19,000
20,000 20,000 20,000 20,000 20,000
Note the question mark in the place of 7 in the second block of text. Things like that still need to be tidied up.
Lastly, you'd copy and paste the rows of text into your spreadsheet etc.
I wanted to post another option I finally found online.
https://convertio.co/es/ocr/
Even though I think K Scandrett answer deserves to be the correct one, since it doesn't rely on a URL, which might go down.
If this is a one-time/rare need and you are windows OS user and you have a Microsoft Excel installed, the application supports extracting the image data to excel. Follow this link for the complete reference.

Numbered reference to figures in Sphinx

Using sphinx 1.3.6, I cannot find a way to make numbered references to figures.
According to answers and comments here: Referencing figures with numbers in Sphinx and reStructuredText,
numref is built into Sphinx.
But this simply does not work:
.. _mylabel
.. figure:: img/fig.jpg
this is the caption
When I use :numref:`Figure %s <mylabel>` in the text, the result is literally Figure %s as if numref was actually not implemented at all.
Do I need something to put in conf.py or in the latex preamble?
I realise this is an old post, however I thought I'd share what helped me in this case.
The following code will add a numbered link to your image with a numbered title. For example the link Fig. 1 will link to the image with the title: Fig. 1 System Process Flow
documentation.rst File:
This is some boring documentation text which refers to the illustration in :numref:`my-image`.
.. figure:: images/process_flow.png
:name: my-image
:align: center
:width: 100%
System Process Flow
conf.py File:
Add this line to your conf.py file:
numfig = True
Your Sphinx doc will now have a link to your numbered figure.
Additionally, page.rst has to be included in some 'toctree'.
Else it will throw errors as follows:
WARNING: no number is assigned for figure:

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")

iTextSharp stamper wraps text

I'm using iTextSharp to fill in some stamper AcroFields.
stamper.AcroFields.SetField("Title", "Lipsum");
I created the pdf in illustrator and the form fields with Adobe Acrobat X Pro. The problem is that although the text fields are the width of the page, in the saved pdf the text wraps at about 1 third of the width.
Another question would be if it's possible the have the textfield autoSize in height, or a way to handle the overflow of the text.
1) I'd like to see that PDF. I suspect the fields aren't as wide as you think they are.
2) You can set a field's font size to zero to enable "auto sizing", which works both within Reader and iText. However, it sizes to the actual field size, not what you think it might be.
I'm guessing you drew a spiffy form field background in Illustrator, then put a field over it in Acrobat Pro, but didn't size the field width to match the spiffy illustrator background. Could be wrong, but that's my hunch.
That's the flattened PDF. Can I see the original with the form field still intact? Sorry I wasn't more specific. None the less, I can learn a little from reading this PDF:
Looking at the bounding boxes for the flattened field XObject and it's internal clipping rectangle, it looks like it should be using most of the page:
The page is ~600 points wide by ~850 tall.
The flattened field XObject is ~560 points wide by ~100 tall.
I wonder if there's some non-standard carriage return characters in your text that iText picks up on by Acrobat does not...
Anyway, I'd like to see the unflattened PDF. Filled in is good, but not flattened.
Okay, looked at the template. I don't see anything that would cause the line breaking you're seeing... which makes me think my second guess was right: new line characters.
Looking at the text layout code might give me a hint. Each of your lines of text goes like this (for example):
1 0 0 1 2 88.24 Tm 0 g (Die Semmerrolle der l{e4}nge nach zu einer grossen Roulade)Tj
n n n n n n Tm: text matrix
g: gray (0 g: black)
(...)Tj: show text
That's consistent with the code path when you set a text field value in the trunk of iText (and the most recent release[s]). That code (ColumnText) is quite good at breaking text properly, and used all over the place. The bounding box is correct (as shown in a couple places of the flattened PDF).
Check your input.

Resources