Vertical headers in RestructuredText tables - restructuredtext

In RestructuredText, you can render a header row in a table like this (taken from the documentation :
+------------------------+------------+----------+----------+
| Header row, column 1 | Header 2 | Header 3 | Header 4 |
| (header rows optional) | | | |
+========================+============+==========+==========+
| body row 1, column 1 | column 2 | column 3 | column 4 |
+------------------------+------------+----------+----------+
| body row 2 | Cells may span columns. |
+------------------------+------------+---------------------+
| body row 3 | Cells may | - Table cells |
+------------------------+ span rows. | - contain |
| body row 4 | | - body elements. |
+------------------------+------------+---------------------+
Is it possible to do the something similar with the first column?
An example, which clearly doesn't work, could be the following (note the double like at the end of column 1):
+------------------------++------------+----------+----------+
| Header row, column 1 || Header 2 | Header 3 | Header 4 |
| (header rows optional) || | | |
+========================++============+==========+==========+
| body row 1, column 1 || column 2 | column 3 | column 4 |
+------------------------++------------+----------+----------+
| body row 2 || Cells may span columns. |
+------------------------++------------+---------------------+
| body row 3 || Cells may | - Table cells |
+------------------------++ span rows. | - contain |
| body row 4 || | - body elements. |
+------------------------++------------+---------------------+

You may achieve this using list-table directive with option stub-columns. Or, you may even combine stub-columns with header-rows. See the http://docutils.sourceforge.net/docs/ref/rst/directives.html#list-table for the details. Hereafter is a simple example:
.. list-table:: Sample list table
:widths: 10 20 20
:header-rows: 1
:stub-columns: 1
* -
- Column 1
- Column 2
* - Row 1
- Hello
- World!
* - Row 2
- Hello
- List Table!
* - Row 3
- This
- Works
An obvious disadvantage is that you need to maintain table content as a list, which may be not that much convenient as with regular simple tables. So, you might want to check out the csv-table directive here: http://docutils.sourceforge.net/docs/ref/rst/directives.html#id1 , which also has option stub-columns.
If you need to stick to regular tables syntax - sorry, I'm not sure this is possible. As a workaround - you can use strong emphasis for text in the first column :-)

Related

SQLite find table row where a subset of columns satisfies a specified constraint

I have the following SQLite table
CREATE TABLE visits(urid INTEGER PRIMARY KEY AUTOINCREMENT,
hash TEXT,dX INTEGER,dY INTEGER,dZ INTEGER);
Typical content would be
# select * from visits;
urid | hash | dx | dY | dZ
------+-----------+-------+--------+------
1 | 'abcd' | 10 | 10 | 10
2 | 'abcd' | 11 | 11 | 11
3 | 'bcde' | 7 | 7 | 7
4 | 'abcd' | 13 | 13 | 13
5 | 'defg' | 20 | 21 | 17
What I need to do here is identify the urid for the table row which satisfies the constraint
hash = 'abcd' AND (nearby >= (abs(dX - tX) + abs(dY - tY) + abs(dZ - tZ))
with the smallest deviation - in the sense of smallest sum of absolute distances
In the present instance with
nearby = 7
tX = tY = tZ = 12
there are three rows that meet the above constraint but with different deviations
urid | hash | dx | dY | dZ | deviation
------+-----------+-------+--------+--------+---------------
1 | 'abcd' | 10 | 10 | 10 | 6
2 | 'abcd' | 11 | 11 | 11 | 3
4 | 'abcd' | 12 | 12 | 12 | 3
in which case I would like to have reported urid = 2 or urid = 3 - I don't actually care which one gets reported.
Left to my own devices I would fetch the full set of matching rows and then dril down to the one that matches my secondary constraint - smallest deviation - in my own Java code. However, I suspect that is not necessary and it can be done in SQL alone. My knowledge of SQL is sadly too limited here. I hope that someone here can put me on the right path.
I now have managed to do the following
CREATE TEMP TABLE h1(v1 INTEGER,v2 INTEGER);
SELECT urid,(SELECT (abs(dX - 12) + abs(dY - 12) + abs(dZ - 12))) devi FROM visits WHERE hash = 'abcd';
which gives
--SELECT * FROM h1
urid | devi |
-------+-----------+
1 | 6 |
2 | 3 |
4 | 3 |
following which I issue
select urid from h1 order by v2 asc limit 1;
which yields urid = 2, the result I am after. Whilst this works, I would like to know if there is a better/simpler way of doing this.
You're so close! You have all of the components you need, you just have to put them together into a single query.
Consider:
SELECT urid
, (abs(dx - :tx) + abs(dy - :tx) + abs(dz - :tx)) AS devi
FROM visits
WHERE hash=:hashval AND devi < :nearby
ORDER BY devi
LIMIT 1
Line by line, first you list the rows and computed values you want (:tx is a placeholder; in your code you want to prepare a statement and then bind values to the placeholders before executing the statement) from the visit table.
Then in the WHERE clause you restrict what rows get returned to those matching the particular hash (That column should have an index for best results... CREATE INDEX visits_idx_hash ON visits(hash) for example), and that have a devi that is less than the value of the :nearby placeholder. (I think devi < :nearby is clearer than :nearby >= devi).
Then you say that you want those results sorted in increasing order according to devi, and LIMIT the returned results to a single row because you don't care about any others (If there are no rows that meet the WHERE constraints, nothing is returned).

sqlite, order by date/integer in joined table

I have two tables
Names
id | name
---------
5 | bill
15 | bob
10 | nancy
Entries
id | name_id | added | description
----------------------------------
2 | 5 | 20140908 | i added this
4 | 5 | 20140910 | added later on
9 | 10 | 20140908 | i also added this
1 | 15 | 20140805 | added early on
6 | 5 | 20141015 | late to the party
I'd like to order Names by the first of the numerically-lowest added values in the Entries table, and display the rows from both tables ordered by the added column overall, so the results will be something like:
names.id | names.name | entries.added | entries.description
-----------------------------------------------------------
15 | bob | 20140805 | added early on
5 | bill | 20140908 | i added this
10 | nancy | 20140908 | i also added this
I looked into joins on the first item (e.g. SQL Server: How to Join to first row) but wasn't able to get it to work.
Any tips?
Give this query a try:
SELECT Names.id, Names.name, Entries.added, Entries.description
FROM Names
INNER JOIN Entries
ON Names.id = Entries.name_id
ORDER BY Entries.added
Add DESC if you want it in reverse order i.e.: ORDER BY Entries.added DESC.
This should do it:
SELECT n.id, n.name, e.added, e.description
FROM Names n INNER JOIN
(SELECT name_id, description, Min(added) FROM Entries GROUP BY name_id, description) e
ON n.id = e.name_id
ORDER BY e.added

Fixed and variable size columns in variable size table

I've looked all over for answers on how to do this, including dozens of answers on Stack Overflow that provide almost but not quite solutions.
I am trying to make a table/list with a number of options. Imagine a table with the following columns:
Delete: A simple icon. This must be a fixed width (because it uses an image)
Name: The name of the item in the list. This should fill the remaining available space, but if the text overflows, I want the ellipsis to appear.
Options A/B/C: You can imagine these are check boxes and also are a fixed with.
So on a wide table it'd look like this:
| X | Item 1 in the list | A | B | C |
| X | Item 2 | A | B | C |
| X | Item 3 has a pretty long name | A | B | C |
| X | Item 4's name is long, realll... | A | B | C |
And on a short table (or say, after the window resized):
| X | Item 1 in the list | A | B | C |
| X | Item 2 | A | B | C |
| X | Item 3 has a pretty... | A | B | C |
| X | Item 4's name is... | A | B | C |
If someone could provide a fiddle showing this in action, that'd be absolutely fantastic.
EDIT: Thank you so much Plymouth!
I've created a fiddle here.
These are the important styles:
table
{
table-layout:fixed;
}
.col2
{
width:auto;
text-overflow:ellipsis;
white-space: nowrap;
overflow: hidden;
}
Is this what you're after?

Multi-line table cell in reStructuredText?

Is there a way to input a newline into a table cell? For example, say I have a table like this:
+==========+==========+==========+
+ Header 1 + Header 2 + Header 3 +
+==========+==========+==========+
+ Item 1 + + +
+ Item 2 + + +
+----------+----------+----------+
I want the above to create a table with two rows, three columns, and the second row, first column to display Item 1 and Item 2 on separate lines.
I have tried the line blocks syntax |, but it doesn't work inside a table cell. I can use list syntax, but I don't want bullet points to appear.
First of all I think your table syntax is incorrect, should it not be:
+----------+----------+----------+
| Header 1 | Header 2 | Header 3 |
+==========+==========+==========+
| Item 1 | | |
| Item 2 | | |
+----------+----------+----------+
Note that the top row is made up of hyphens, not equal signs, and the rows are separated by pipes, |, not plus signs.
Now with this table, the line block syntax:
+----------+----------+----------+
| Header 1 | Header 2 | Header 3 |
+==========+==========+==========+
| | Item 1 | | |
| | Item 2 | | |
+----------+----------+----------+
seems to work: testing with Pandoc the bottom left cell gets transformed into the following HTML:
<td align="left">Item 1<br />Item 2</td>
Note the line break <br /> in between Item 1 and Item 2.
You can also leave a gap between the lines like this
+----------+----------+----------+
| Header 1 | Header 2 | Header 3 |
+==========+==========+==========+
| Item 1 | | |
| | | |
| Item 2 | | |
+----------+----------+----------+
This method tends to be friendlier with editors so they dont think you have accidentally added an extra pipe
I use the following syntax to create tables including multiline cells with sphinx:
.. list-table::
* - **HEADER1**
- **HEADER2**
- **HEADER3**
* - TEXT 1
- | MULTILINE
| TEXT
- | MULTILINE
| TEXT 2
I use line blocks with beginning | to preserve the line-breaks.

referencing tables in restructuredtext

Is there anyway to refer a table in RestructuredText? something like see table `referencetable`_
(I saw some workarounds for referencing figures. couldn't find a way to refer a table though .. )
Thanks!
You can simply define a hyperlink target.
Here is table-1_.
.. _table-1:
+------------+------------+-----------+
| Header 1 | Header 2 | Header 3 |
+============+============+===========+
| body row 1 | column 2 | column 3 |
+------------+------------+-----------+
| body row 2 | Cells may span columns.|
+------------+------------+-----------+
| body row 3 | Cells may | - Cells |
+------------+ span rows. | - contain |
| body row 4 | | - blocks. |
+------------+------------+-----------+

Resources