Sphinx references to other sections containing section number and section title - latex

I am using Sphinx to write a document with lots of references:
.. _human-factor:
The Human Factor
================
...
(see :ref:`human-factor` for details)
The compiled document contains something like this:
(see The Human Factor for details)
Instead I would like to have it formatted like this:
(see 5.1 The Human Factor for details)
I tried to google the solution and I found out that the latex hyperref package can do this but I have no idea how to add this to the Sphinx build.

I resolved it by basically using numsec.py from here: https://github.com/jterrace/sphinxtr
I had to replace the doctree_resolved function with this one to get section number + title (e.g. "5.1 The Human Factor").
def doctree_resolved(app, doctree, docname):
secnums = app.builder.env.toc_secnumbers
for node in doctree.traverse(nodes.reference):
if 'refdocname' in node:
refdocname = node['refdocname']
if refdocname in secnums:
secnum = secnums[refdocname]
emphnode = node.children[0]
textnode = emphnode.children[0]
toclist = app.builder.env.tocs[refdocname]
anchorname = None
for refnode in toclist.traverse(nodes.reference):
if refnode.astext() == textnode.astext():
anchorname = refnode['anchorname']
if anchorname is None:
continue
linktext = '.'.join(map(str, secnum[anchorname]))
node.replace(emphnode, nodes.Text(linktext
+ ' ' + textnode))
To make it work one needs to include the numsec extension in conf.py and also to add :numbered: in the toctree like so:
.. toctree::
:maxdepth: 1
:numbered:

Related

Using Insert with a large multi-layered table using Lua

So I am working on a script for GTA5 and I need to transfer data over to a js script. However so I don't need to send multiple arrays to js I require a table, the template for the table should appear as below.
The issue I'm having at the moment is in the second section where I receive all vehicles and loop through each to add it to said 'vehicleTable'. I haven't been able to find the "table.insert" method used in a multilayered table
So far I've tried the following
table.insert(vehicleTable,vehicleTable[class][i][vehicleName])
This seems to store an 'object'(table)? so it does not show up when called in the latter for loop
Next,
vehicleTable = vehicleTable + vehicleTable[class][i][vehicleName]
This seemed like it was going nowhere as I either got a error or nothing happened.
Next,
table.insert(vehicleTable,class)
table.insert(vehicleTable[class],i)
table.insert(vehicleTable[class][i],vehicleName)
This one failed on the second line, I'm unsure why however it didn't even reach the next problem I saw later which would be the fact that line 3 had no way to specify the "Name" field.
Lastly the current one,
local test = {[class] = {[i]={["Name"]=vehicleName}}}
table.insert(vehicleTable,test)
It works without errors but ultimately it doesn't file it in the table instead it seems to create its own branch so object within the object.
And after about 3 hours of zero progress on this topic I turn to the stack overflow for assistance.
local vehicleTable = {
["Sports"] = {
[1] = {["Name"] = "ASS", ["Hash"] = "Asshole2"},
[2] = {["Name"] = "ASS2", ["Hash"] = "Asshole1"}
},
["Muscle"] = {
[1] = {["Name"] = "Sedi", ["Hash"] = "Sedina5"}
},
["Compacts"] = {
[1] = {["Name"] = "MuscleCar", ["Hash"] = "MCar2"}
},
["Sedan"] = {
[1] = {["Name"] = "Blowthing", ["Hash"] = "Blowthing887"}
}
}
local vehicles = GetAllVehicleModels();
for i=1, #vehicles do
local class = vehicleClasses[GetVehicleClassFromName(vehicles[i])]
local vehicleName = GetLabelText(GetDisplayNameFromVehicleModel(vehicles[i]))
print(vehicles[i].. " " .. class .. " " .. vehicleName)
local test = {[class] = {[i]={["Name"]=vehicleName}}}
table.insert(vehicleTable,test)
end
for k in pairs(vehicleTable) do
print(k)
-- for v in pairs(vehicleTable[k]) do
-- print(v .. " " .. #vehicleTable[k])
-- end
end
If there is not way to add to a library / table how would I go about sorting all this without needing to send a million (hash, name, etc...) requests to js?
Any recommendations or support would be much appreciated.
Aside the fact that you do not provide the definition of multiple functions and tables used in your code that would be necessary to provide a complete answere without making assumptions there are many misconceptions regarding very basic topics in Lua.
The most prominent is that you don't know how to use table.insert and what it can do. It will insert (append by default) a numeric field to a table. Given that you have non-numeric keys in your vehicleTable this doesn't make too much sense.
You also don't know how to use the + operator and that it does not make any sense to add a table and a string.
Most of your code seems to be the result of guess work and trial and error.
Instead of referring to the Lua manual so you know how to use table.insert and how to index tables properly you spend 3 hours trying all kinds of variations of your incorrect code.
Assuming a vehicle model is a table like {["Name"] = "MyCar", ["Hash"] = "MyCarHash"} you can add it to a vehicle class like so:
table.insert(vehicleTable["Sedan"], {["Name"] = "MyCar", ["Hash"] = "MyCarHash"})
This makes sense because vehicleTable.Sedan has numeric indices. And after that line it would contain 2 cars.
Read the manual. Then revisit your code and fix your errors.

make4ht with biblatex fails for records using \url when title field used but not when title field eliminated

When I use make4ht (or htlatex) to compile a .tex file containing a biblatex bibliography, I get the error:
! Illegal parameter number in definition of \blx#tempa.
<to be read again>
1
l.19 \printbibliography[heading=bibintoc]
This error occurs when the bibliography record in the .bib file contains a url (I use \url{link is here}), and appears to be related to how many fields the record has, such as title, published (or howpublished for #misc), because this error does not occur when I make a much simpler bibliography record without many fields.
Also, I have no troubles with the bibliography when I compile for latex.
MWE (main_test_file.tex):
\documentclass[11pt]{article}
% Use Chicago Manual of Style:
\usepackage[authordate,autocite=inline,backend=biber,natbib]{biblatex-chicago}
\usepackage[colorlinks]{hyperref}
% References file:
\addbibresource{bib_test_file.bib}
%
\begin{document}
Some writing stuff: \autocite{trialurl1} works with make4ht when no extra stuff before $\backslash$url, but doesn't work when add another feature, like a title or howpublished, etc.
% Uncomment the following line, and the make4ht fails:
, as in \cite{trialurl2}.
More interesting stuff: \autocite{vanier} should have no problems with make4ht.
% The list of references is printed:
\printbibliography[heading=bibintoc]
\end{document}
The following fails with the above-mentioned error when trialurl2 is cited:
make4ht -ue mybuild.mk4 main_test_file.tex
but works fine when trialurl2 is not cited. In both cases, it doesn't matter whether I use \cite, \autocite, \citeauthor, etc. The same behavior occurs.
Also, using latex, then biber, then latex, then latex, works fine, even when trialurl2 is cited.
The bib_test_file.bib file is:
#misc{trialurl1,
author = {George, Birdie},
note = {\url{https://mail.yahoo.com/} Accessed 24 July 2020},
year = {2020},
}
#misc{trialurl2,
author = {George, Birdie},
title = {Hi},
note = {\url{https://mail.yahoo.com/} Accessed 24 July 2020},
year = {2020},
}
#book{vanier,
title = {Living Gently in a Violent World: The Prophetic Witness of Weakness},
author = {Vanier, Jean and Hauerwas, Stanley},
edition = {Second},
year = {2018},
publisher = {InterVarsity Press},
}
and the mybuild.mk4 is
Make:add("biber","biber ${input}")
if mode=="draft" then
Make:htlatex {}
else
Make:htlatex {}
Make:biber {}
Make:htlatex {}
Make:htlatex {}
Make:htlatex {}
end
mybuild.mk4 is taken from the answer by michael.h21 here
https://tex.stackexchange.com/questions/244828/illegal-parameter-with-biblatex
michael.h21's answer helped me with some other problems but not my current problem, by the way.
Apparently, for biblatex, the .bib file needs changed (see answer by moewe here:
https://tex.stackexchange.com/questions/345175/bibtex-url-problem
even though the question itself there is not related).
Instead of:
note = {\url{https://mail.yahoo.com/} Accessed 24 July 2020},
I should have put:
url = {https://mail.yahoo.com/},
urldate = {2020-07-24},
So the .bib file should be:
#misc{trialurl1,
author = {George, Birdie},
url = {https://mail.yahoo.com/},
urldate = {2020-07-24},
year = {2020},
}
#misc{trialurl2,
author = {George, Birdie},
title = {Hi},
url = {https://mail.yahoo.com/},
urldate = {2020-07-24},
year = {2020},
}
#book{vanier,
title = {Living Gently in a Violent World: The Prophetic Witness of Weakness},
author = {Vanier, Jean and Hauerwas, Stanley},
edition = {Second},
year = {2018},
publisher = {InterVarsity Press},
}
Then all is well.

Highlight one specific author when generating references in Pandoc

I am using Pandoc to generate a list of publications for my website. I'm using it solely to generate the html with the publications so that I can then paste the raw html in jekyll. This part works fine.
The complications arise when I tty to generate the html so that my name appears boldfaced in all entries. I'm trying to use this solution for that, which works when I apply it to a pure Latex document I am generating. However when I try to apply the same Pandoc, the html is generated without any boldface.
Here's my Pandoc file:
---
bibliography: /home/tomas/Dropbox/cv/makerefen4/selectedpubs.bib
nocite: '#'
linestretch: 1.5
fontsize: 12pt
header-includes: |
\usepackage[
backend=biber,
dashed=false,
style=authoryear-icomp,
natbib=true,
url=false,
doi=true,
eprint=false,
sorting=ydnt, %Year (Descending) Name Title
maxbibnames=99
]{biblatex}
\renewcommand{\mkbibnamegiven}[1]{%
\ifitemannotation{highlight}
{\textbf{#1}}
{#1}}
\renewcommand*{\mkbibnamefamily}[1]{%
\ifitemannotation{highlight}
{\textbf{#1}}
{#1}}
...
And here's the relevant part of my Makefile:
PANDOC_OPTIONS=--columns=80
PANDOC_HTML_OPTIONS=--filter=pandoc-citeproc --csl=els-modified.csl --biblatex
Again: this code generates the references fine. It just doesn't boldface anything as it is supposed to.
Any ideas?
EDIT
Bib entries look like this
#MISC{test,
AUTHOR = {Last1, First1 and Last2, First2 and Last3, First3},
AUTHOR+an = {2=highlight},
}
And versions are
- Biblatex 3.12
- Biber 2.12
You can use a lua filter to modify the AST. The following works for me to get the surname and initials (Smith, J.) highlighted in the references (see here). You can replace pandoc.Strong with pandoc.Underline or pandoc.Emph. Replace Smith and J. with your name/initials, save it as myname.lua and use something like:
pandoc --citeproc --bibliography=mybib.bib --csl.mycsl.csl --lua-filter=myname.lua -o refs.html refs.md
i.e. put the filter last.
local highlight_author_filter = {
Para = function(el)
if el.t == "Para" then
for k,_ in ipairs(el.content) do
if el.content[k].t == "Str" and el.content[k].text == "Smith,"
and el.content[k+1].t == "Space"
and el.content[k+2].t == "Str" and el.content[k+2].text:find("^J.") then
local _,e = el.content[k+2].text:find("^J.")
local rest = el.content[k+2].text:sub(e+1)
el.content[k] = pandoc.Strong { pandoc.Str("Smith, J.") }
el.content[k+1] = pandoc.Str(rest)
table.remove(el.content, k+2)
end
end
end
return el
end
}
function Div (div)
if 'refs' == div.identifier then
return pandoc.walk_block(div, highlight_author_filter)
end
return nil
end
Notes:
The above works if you use an author-date format csl. If you want to use a numeric format csl (e.g. ieee.csl or nature.csl) you will need to substitute Span for Para in the filter, i.e.:
Span = function(el)
if el.t == "Span" then
If you also want to use the multiple-bibliographies lua filter, it should go before the author highlight filter. And 'refs' should be 'refs_biblio1' or 'refs_biblio2' etc, depending on how you have defined them:
function Div (div)
if 'refs' or 'refs_biblio1’ or 'refs_biblio2’ == div.identifier then
For pdf output, you will also need to add -V csl-refs in the pandoc command if you use a numeric format csl.
The filter highlights Smith, J. if formated in this order by the csl. Some csl will use this format for the first author and then switch to J. Smith for the rest, so you will have to adjust the filter accordingly adding an extra if el.content[k].t == "Str”…etc. Converting to .json first will help to check the correct formatting in the AST.

How to use LaTeX section numbers in Pandoc cross-reference

The Pandoc documentation says that cross references can be made to section headers in a number of ways. For example, you can create your own ID and reference that ID. For example:
# This is my header {#header}
Will create an ID with value '#header' that can be refenced in the text, as such:
[Link to header](#header)
Which will display the text 'Link to header' with a link to the header.
I couldn't find anywhere how to make the text of the link be the section number when compiled as a LaTeX document.
For example, if my header is compiled to '1.2.3 Section Header', I want my cross-reference to text to display as '1.2.3'.
This can be achieved by defining the ID as done previously. eg:
# This is my header {#header}
Then in the text, the cross reference can be written as:
\ref{header}
When this compiles to LaTeX, the cross-reference text will be the section number of the referenced heading.
You can use the pandoc-secnos filter, which is part of the pandoc-xnos filter suite.
The header
# This is my header {#sec:header}
is referenced using #sec:header. Alternatively, you can reference
# This is my header
using #sec:this-is-my-header.
Markdown documents coded in this way can be processed by adding --filter pandoc-secnos to the pandoc call. The --number-sections option should be used as well. The output uses LaTeX's native commands (i.e., \label and \ref or \cref).
The benefit to this approach is that output in other formats (html, epub, docx, ...) is also possible.
A general solution which works with all supported output formats can be build by leveraging pandoc Lua filters: The function pandoc.utils.hierarchicalize can be used to get the document hierarchy. We can use this to associate section IDs with section numbers, which can later be used to add these numbers to links with no link description (e.g., [](#myheader)).
local hierarchicalize = (require 'pandoc.utils').hierarchicalize
local section_numbers = {}
function populate_section_numbers (doc)
function populate (elements)
for _, el in pairs(elements) do
if el.t == 'Sec' then
section_numbers['#' .. el.attr.identifier] = table.concat(el.numbering, '.')
populate(el.contents)
end
end
end
populate(hierarchicalize(doc.blocks))
end
function resolve_section_ref (link)
if #link.content > 0 or link.target:sub(1, 1) ~= '#' then
return nil
end
local section_number = pandoc.Str(section_numbers[link.target])
return pandoc.Link({section_number}, link.target, link.title, link.attr)
end
return {
{Pandoc = populate_section_numbers},
{Link = resolve_section_ref}
}
The above should be saved to a file and then passed to pandoc via the --lua-filter option.
Example
Using the example from the question
# This is my header {#header}
## Some subsection
See section [](#header), especially [](#some-subsection)
Using the above filter, the last line will render as "See section 1, especially 1.1".
Don't forget to call pandoc with option --number-sections, or headers will not be numbered.
Since pandoc version 2.8 the function pandoc.utils.hierarchicalize has been replaced with make_sections. Here is an updated version of the #tarleb's answer which works with newer ´pandoc´ versions.
local make_sections = (require 'pandoc.utils').make_sections
local section_numbers = {}
function populate_section_numbers (doc)
function populate (elements)
for _, el in pairs(elements) do
if el.t == 'Div' and el.attributes.number then
section_numbers['#' .. el.attr.identifier] = el.attributes.number
populate(el.content)
end
end
end
populate(make_sections(true, nil, doc.blocks))
end
function resolve_section_ref (link)
if #link.content > 0 or link.target:sub(1, 1) ~= '#' then
return nil
end
local section_number = pandoc.Str(section_numbers[link.target])
return pandoc.Link({section_number}, link.target, link.title, link.attr)
end
return {
{Pandoc = populate_section_numbers},
{Link = resolve_section_ref}
}

Stanford type dependency, can not extract "prepositional modfier"

I am trying to extract the prepositional modifier, like it is stated in the Dependency Manual:
I try to parse the sentence :
"I saw a cat with a telescope" , using the code:
List<CoreMap> sentences = stanfordDocument.get(SentencesAnnotation.class);
for (CoreMap sentence : sentences) {
Tree tree = sentence.get(TreeAnnotation.class);
TreebankLanguagePack languagePack = new PennTreebankLanguagePack();
GrammaticalStructureFactory grammaticalStructureFactory = languagePack.grammaticalStructureFactory();
GrammaticalStructure structure = grammaticalStructureFactory.newGrammaticalStructure(tree);
Collection<TypedDependency> typedDependencies = structure.typedDependenciesCollapsed();
for (TypedDependency td : typedDependencies) {
System.out.println(td.reln());
}
}
As stated in the Manual I was expecting to get : prep(saw, with).
In the Collection of the TypedDependeny I get only
"nsubj; root; det; dobj; det; prep_with" as relation type, and not the "prep/prepc" as stated in the http://robotics.usc.edu/~gkoch/DependencyManual.pdf (page 8).
I have also tried to extract pcomp : Prepositional compelement (page 7 of the manual) and it doesnt find it.
Did somebody encountered the same problem? Am I doing anything wrong?
CoreNLP outputs "Collapsed dependencies preserving a tree structure" (section 4.4 of the manual) from my experience. I think it's the same thing here (e.g. prep_with is a collapsed dependency of prep(saw, with))

Resources