include the word "table" in List of Tables, "Appendix" in Table of Contents, etc - latex

I need to include the word "Table" at the beginning of each line in my List of Tables. That is, instead of:
LIST OF TABLES
1 The first table ........... 10
2 The second table ........... 20
I need it to say:
LIST OF TABLES
Table 1 The first table ........... 10
Table 2 The second table ........... 20
Yes, I know that's ugly, but those are the rules.
I also need the Table of contents to say:
Table of Contents
1 The first Chapter ...... 1
Appendices
Appendix A The A appendix ........ 10
Any idea how to do this in a simple and consistent manner?

To answer your three questions:
1: Table prefix in the list of tables put the following in your preamble:
\usepackage{tocloft}
\newlength\tablelen
\settowidth\tablelen{Table}
\addtolength\cfttabnumwidth{\tablelen}
\renewcommand\cfttabpresnum{Table }
2: To have "Appendices" appear in your table of contents put the following just after your call to \appendix:
\addcontentsline{toc}{chapter}{Appendices}
3: To have "Appendix" as a prefix for each appendix in the table of contents, see:
http://for.mat.bham.ac.uk/pgweb/thesisfiles/bhamthesisman.pdf
http://for.mat.bham.ac.uk/pgweb/thesisfiles/bhamthesis.dtx
in particular, search for his \renewcommand{\appendix} in which add to contents is changed.

The easier way is to replace the word \listoftables with
{%
\let\oldnumberline\numberline%
\renewcommand{\numberline}{\tablename~\oldnumberline}%
\listoftables%
}

Related

Multiple LaTeX bibliographies each in descending chronological order with reverse numbering

I'm working on my CV in moderncv latex document, and my plan is to have separate lists for
publications (type=article),
data sets (type=misc), and
presentations (type=inproceedings)
I'm looking for someone wiser to tell me, if it is indeed possible to have
three bibliographies in a single document,
each of them in descending chronological order (newest first), and
with reverse numbering?
i.e.
Section: Publications
[9.] Publication (newest)
[8.] Publication (second newest)
...
[1.] Publication (oldest)
Section: Data sets
[5.] Data set (newest)
[4.] Data set (second newest)
...
[1.] Data set (oldest)
Section: Presentations
[11.] Presentation (newest)
[10.] Presentation (second newest)
...
[1.] Presentation (oldest)
So far I have managed to make one single bibliography in descending chronological order with reverse numbering for my publications (thanks internet!). If I add other entries in the same bib file and separate them by keyword or type, the numbering of the separate lists is screwed up. In that case all entries are first put in descending chronological order and assigned numbers, but only then split into separate lists. Therefore the numbering in each list is not consecutive anymore.
Here's what I used in the preamble:
\usepackage[backend=bibtex,style=numeric,sorting=ydnt,url=false,doi=true,maxnames=120]{biblatex}
% Count total number of entries in each refsection
\AtDataInput{%
\csnumgdef{entrycount:\therefsection}{%
\csuse{entrycount:\therefsection}+1}}
% Print the labelnumber as the total number of entries in the
% current refsection, minus the actual labelnumber, plus one
\DeclareFieldFormat{labelnumber}{\mkbibdesc{#1}}
\newrobustcmd*{\mkbibdesc}[1]{%
\number\numexpr\csuse{entrycount:\therefsection}+1-#1\relax}
\addbibresource{bibliography.bib}
In the main document itself, I use
\nocite{*}
\printbibheading[title={Publications}]
\printbibliography[title={Peer-reviewed},heading=subbibliography, type=article]
\printbibliography[title={Data},heading=subbibliography, type=misc]
\section{Presentations}
\printbibliography[heading=none, type=inproceedings]
Stack Overflow, I'm counting on you!

Problems with lua filter to iterate over table rows

I'm trying to write a simply lua filter for pandoc in order to do some macro expansion for the elements in a ReST Table.
filter.lua
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function Table(table)
elems=pandoc.Table(table)["rows"]
print(tablelength(table))
for v in pairs(elems) do
print(v) -- Prints nothings
end
return table
end
test.rst
======= =========
A B
======= =========
{{x}} {{y}}
======= =========
Now, if I run pandoc.exe -s --lua-filter filter.lua test.rst -t rst the program says that there are 5 elements in elems, but the for loop is just skipped and I really don't know what I'm doing wrong here.
I'm very new to Lua and also know pandoc very litte. How can I iterate over the elements in elems?
Pandoc lua-filters provide the handy walk_block helper, that recursively walks the document tree down and applies a function to the elements that match the key.
In the example below, we give walk_block a lua table (what's a map or dict in other languages) with only one key (the key Str), and the value of the table is the function to apply. The function checks for the braces, strips them and prepends foo.
function Table(table)
return pandoc.walk_block(table, {
Str = function(el)
if el.text:sub(1,2) == '{{' then
txt = 'foo' .. el.text:sub(3, -3)
else
txt = el.text
end
return pandoc.Str(txt)
end
})
end
There are a couple of areas of misunderstanding in your code. First you need to remember that everything in lua is a table (implemented as an associative array or dictionary), and arrays are just a special case of a table where the keys are integers. To avoid confusion, for the rest of this answer I will use Table when I'm referring to the pandoc document element, and table when I'm referring to the lua data structure.
Your tablelength function is just counting the number of elements in the pandoc table that represents a Table. If you look in https://www.pandoc.org/lua-filters.html#type-ref-Block you will see that a Table has 5 properties- caption, aligns, widths, headers and rows. The return value of this function is 5 because of this. If you print out the values in the loop inside tablelength then you will confirm this. If you want to count the rows then you will need to pass the rows array into the function, not the table.
The second problem is that you are creating a new table rather than using the one passed in by pandoc. Instead of using elems=pandoc.Table(table)["rows"] just use elems=table["rows"] or elems=table.rows which is equivalent. The function pandoc.Table() is used to create a new element.
Furthermore, to loop over the elements in a table that are in the form of an array, you can use the ipairs function- it will return the numerically indexed values as described here What is the difference of pairs() vs. ipairs() in Lua?.
The rows table is, as might be expected, an array of rows where each row is in turn an array of elements. So to access the elements of the table you will need to have two loops.
Finally there is the issue of the pandoc object model. Because a table can contain other things (images, links, bold text etc) the final cell value is actually a list of blocks. Now depending on what you want to do with the table you can deal with this in different ways. You can use the walk_block function that mb21 referred to, but looping over just the blocks in a single cell. If your Table only contains (unformatted) text then you can simplify things by using the stringify function, which collapses the list of blocks into a single string.
Putting all of this together gives the following modified version of your code.
local stringify=pandoc.utils.stringify
-- This function is no longer needed
function tablelength(T)
local count = 0
for e in pairs(T) do
count = count + 1
print(e) -- this shows the key not the value
end
return count
end
function Table(table)
rows=table["rows"]
print("TableLength="..#rows)
for rownum,row in ipairs(rows) do
for colnum, elem in ipairs(row) do
print(stringify(elem)) -- Prints cell text
end
end
return table
end
As to you followup question, if you want to modify things then you just need to replace the cell values, while respecting pandoc's object model. You can construct the various types used by pandoc with the constructors in module pandoc (such as the aforementioned pandoc.Table). The simplest table cell will be an array with a single Plain block, which in turn contains a single Str element (blocks usually contain a list of Inline elements).
The following code shows how you can modify a table using the existing content or the row/column number. Note that I changed the parameter to the Table function from table to tableElem because table is a common type used in lua and overriding it gives hard to track errors.
local stringify=pandoc.utils.stringify
function makeCell(s)
return {pandoc.Plain({pandoc.Str(s)})}
end
function Table(tableElem)
rows=tableElem["rows"]
for rownum,row in ipairs(rows) do
for colnum, elem in ipairs(row) do
local elemText=stringify(elem)
if elemText=="{{x}}" then
row[colnum]=makeCell(elemText:gsub("x","newVal"))
end
if rownum==1 and colnum==2 then
row[colnum]=makeCell("Single cell")
end
end
end
local newRow={ makeCell("New A"), makeCell("New B")}
table.insert(rows,newRow)
return tableElem
end

referencing latex document in order of appearance

Suppose I have an ordered bibliography list
\bibitem{1} biblio 1
\bibitem{2} biblio 2
\bibitem{3} biblio 3
and suppose the first line in my text refers to biblio 3. Is there a way to make the refs follows the order of appearance or am I suppose to reorder the whole bibitems ?
Your new friend is bibtex or biblatex (the second is my pref.). The automatic order of references is just the top of the ice berg.
https://www.ctan.org/pkg/bibtex
https://www.ctan.org/pkg/biblatex

How to sort a list of 1million records by the first letter of the title

I have a table with 1 million+ records that contain names. I would like to be able to sort the list by the first letter in the name.
.. ABCDEFGHIJKLMNOPQRSTUVWXYZ
What is the most efficient way to setup the db table to allow for searching by the first character in the table.name field?
The best idea right now is to add an extra field which stores the first character of the name as an observer, index that field and then sort by that field. Problem is it's no longer necessarily alphabetical.
Any suggestions?
You said in a comment:
so lets ignore the first letter part. How can I all records that start with A? All A's no B...z ? Thanks – AnApprentice Feb 21 at 15:30
I issume you meant How can I RETURN all records...
This is the answer:
select * from t
where substr(name, 1, 1) = 'A'
I agree with the questions above as to why you would want to do this -- a regular index on the whole field is functionally equivalent. PostgreSQL (with some new ones in v. 9) has some rather powerful indexing capabilities for special cases which you might want to read about here http://www.postgresql.org/docs/9.1/interactive/sql-createindex.html

How do I parse a table into its meaningful chunks?

I need to extract a table of data on a collection of pages. I can already traverse the pages just fine.
How do I extract the table's data? I'm using Ruby and Nokogiri, but I would assume that this is a pretty general issue.
I underlined the desired data points in each row in the following image.
A sample of the html is: http://pastebin.com/YYFPbFLC
How would I parse this table into a hash via Nokogiri into the meaningful chunks?
The table's xpath is:
/html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr/td[2]/table
The table has a variable number of rows of data and formatting rows. I only want to collect the rows with meaningful data, but I don't readily see a way to distinguish this via an XPath except the second column will reliably have "keyword" in it. Each of these rows have an XPath of:
1st meaningful row is: /html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr/td[2]/table/tbody/tr[2]
...
Last meaningful row: /html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr/td[2]/table/tbody/tr[N]
The first meaningful column that needs to match text content on the "keyword" is:
/html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[2]
The last column of this first row of data would be:
/html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[6]
Each row is a record and has a timestamp with this column/td being the time in the timestamp; The year, month and day are all in their own variables and can be appended for a full timestamp:
/html/body/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/table/tbody/tr/td/table/tbody/tr/td[2]/table/tbody/tr[2]/td[5]
The first rule of XPath is: never use the autogenerated XPath from Firebug or other browser tool. This creates brittle XPath that treats all page elements as equally important and required, even parts you don't care about. For example, if a notice went up at the top of the page and it happened to be in a table, it could throw off your parsing.
Instead, think about how a human would identify it. In this case, you want "the first table under the heading with the word 'today' in it". Here's the XPath for that:
//table[preceding-sibling::h2[contains(text(), "today")]][1]
This says take the tables that have a preceding h2 (in other words, that follow the h2), where the h2 contains the word "today". Then take the first such table.
Then you need to identify the rows you are interested in. Note that some rows are just dividers containing a single td, so you want to make sure you only parse the rows that have multiple td tags. In XPath, that is:
//tr[td[2]]
Then you just grab the content of all the columns. In the first one you can remove everything before the words "of magnitude" to get just the value. Putting it all together:
doc = Nokogiri::HTML.parse(html)
events = []
doc.xpath('//table[preceding-sibling::h2[contains(text(), "today")]][1]//tr[td[2]]').each do |row|
cols = row.search('td/text()').map(&:to_s)
events << {
:magnitude => cols[0].gsub(/^.*of magnitude /,''),
:temp_area => cols[1],
:time_start => cols[2],
:time_middle => cols[3],
:time_end => cols[4]
}
end
The output is:
[
{:magnitude=>"F1.7",
:temp_area=>"0",
:time_start=>"01:11:00",
:time_middle=>"01:24:00",
:time_end=>"01:32:00"},
{:magnitude=>"F3.1",
:temp_area=>"0",
:time_start=>"04:01:00",
:time_middle=>"04:10:00",
:time_end=>"04:26:00"},
{:magnitude=>"F3.5",
:temp_area=>"134F55",
:time_start=>"06:24:00",
:time_middle=>"06:42:00",
:time_end=>"06:53:00"},
{:magnitude=>"F1.4",
:temp_area=>"0",
:time_start=>"11:58:00",
:time_middle=>"12:06:00",
:time_end=>"12:16:00"},
{:magnitude=>"F1.0",
:temp_area=>"0",
:time_start=>"13:02:00",
:time_middle=>"13:05:00",
:time_end=>"13:09:00"},
{:magnitude=>"D53.7",
:temp_area=>"134F55",
:time_start=>"17:37:00",
:time_middle=>"18:37:00",
:time_end=>"18:56:00"}
]

Resources