Pandoc template: different separator for last item - templates

I'm writing a custom Pandoc template and want it to print a list of authors so that all authors are separated with ", " except for the last author who instead is separated with an "and". Can this be done?
Let's say this is my template:
$if(title)$
$title$
$endif$
$if(author)$
$for(author)$$author$$sep$, $endfor$
$endif$
$body$
Which outputs:
My title
Author 1, Author 2, Author 3
Document body
But I want it to output:
My title
Author 1, Author 2 and Author 3
Document body

You have to use so-called "pipes" from Pandoc template syntax (see https://pandoc.org/MANUAL.html).
We'll make a for loop for all-but-last items in our array (it is considered as a pronoun inside the loop, refering to the values from authors/allbutlast). For those elements of our authors array, we'll use ", " as a separator, but outside the loop, we'll simply put the "final separator" ("and" in this case), and the last element of authors array (~ stands for unbreakable space, if you are going to output to LaTeX).
$for(authors/allbutlast)$$it$$sep$, $endfor$ and~$authors/last$
EDIT:
In case there's only one author, use something like this:
$if(author/allbutlast)$
$for (author/allbutlast)$
${it}$sep$,
$endfor$
and ${author/last}
$else$
${author/last}
$endif$

I changed Jan's answer a little to get it to work in my use-case (a Quarto template). Why do I have by-author? Not sure, but that is what the Quarto template I am changing uses.
$if(by-author/allbutlast)$
$for(by-author/allbutlast)$
{\large{$by-author.name.literal$}}$sep$,
$endfor$
$for(by-author/last)$
{and \large{$by-author.name.literal$}}
$endfor$
$else$
${by-author/last}
$endif$
Here is my full use case. I know this is way more than what was asked, but if you end up on this answer and are making pandoc templates, this might be helpful. I've learned a lot from seeing full use cases. I am working on a title page template for a Quarto book.
YAML _quarto.yml
project:
type: book
book:
title: The title
subtitle: The subtitle
author:
- name: Jane Doe
- name: Eva Nováková
- name: Matti Meikäläinen
chapters:
- index.qmd
- chap1.qmd
- chap2.qmd
- references.qmd
bibliography: references.bib
format:
pdf:
documentclass: scrbook
template-partials: ["partials/before-body.tex"]
toc: true
include-in-header:
text: |
\newcommand*{\plogo}{\fbox{$\mathcal{PL}$}} % logo
\usepackage[utf8]{inputenc} % international characters
\usepackage[T1]{fontenc} % international characters
\usepackage{hyphenat} % don't hyphenate the title
\usepackage{authblk}
partials/before-body.tex
\begin{frontmatter} % why not titlepage? dunno, titlepage threw errors.
\raggedleft % Right align the title page
\rule{1pt}{\textheight} % Vertical line
\hspace{0.05\textwidth} % Whitespace between the vertical line and title page text
\parbox[b]{0.85\textwidth}{ % Paragraph box for holding the title page
{\large\bfseries\nohyphens{$title$}}\\[2\baselineskip] % Title
$if(subtitle)$
{\large\textit{$subtitle$}}\\[4\baselineskip] % Subtitle
$endif$
$if(by-author/allbutlast)$
$for(by-author/allbutlast)$
{\large{$by-author.name.literal$}}$sep$,
$endfor$
$for(by-author/last)$
{and \large{$by-author.name.literal$}}
$endfor$
$else$
${by-author/last}
$endif$
\vspace{0.5\textheight} % Whitespace
{\noindent The Publisher~~\plogo}\\[\baselineskip] % Publisher+logo
}
\end{frontmatter}

Related

Reduce spacing between title, author and date in knitR document and shift title further up the page

The following is the default way to create a title in R markdown.
title: "Title"
author: "Author"
date: "19/02/2020"
output: pdf_document
The problem is that the 3 lines (title, author, date) occupy too much space. I would like to know a way to reduce the font size of all 3 lines, reduce the margin between the lines, and shift the lines a bit higher up the page. Thanks.

change text of authors name for citations in vitae R package

I am slowly getting a hang of the vitae R-package which incorporates LaTeX and R Markdown into templates for CVs and resumes. In the templates, a .bib files can be used to list out publications. I was wondering if anyone knew of a way to change the text of a specific author in the list in the output. For instance in a list of three authors:
author 1, author 2 and author 3, Year, Title, etc. to
author 1, author 2 and author 3, Year, Title, etc.
I guess you were using vitae::bibliography_entries("*.bib"), not the function vitae::detailed_entries() for your publications, right? If so, the simplest (dirty) way is to put keep_tex: true in the YAML, so after Kniting your *.Rmd file you'll get a *.tex file (let's name it cv.tex). Then run the code below:
library(stringr)
library(magrittr)
readr::read_lines("cv.tex") %>%
str_replace_all("Your Name", "\\\\textbf{Your Name}") %>%
readr::write_lines("cv_bold_name.tex")
In the cv_bold_name.tex file, you'll find "Your Name" is bold by the code: \textbf{Your Name}. Then compile cv_bold_name.tex in Latex, for example in my case I compile it in TeXstudio, you'll get the bold name you want.

Adding a space between paragraphs when extracting text with BeautifulSoup

I need to extract useful text from news articles. I do it with BeautifulSoup but the output sticks together some paragraphs which prevents me from analysing the text further.
My code:
import requests
from bs4 import BeautifulSoup
r = requests.get("http://www.bbc.co.uk/news/uk-england-39607452")
soup = BeautifulSoup(r.content, "lxml")
# delete unwanted tags:
for s in soup(['figure', 'script', 'style']):
s.decompose()
article_soup = [s.get_text() for s in soup.find_all(
'div', {'class': 'story-body__inner'})]
article = ''.join(article_soup)
print(article)
The output looks like this (just first 5 sentences):
The family of British student Hannah Bladon, who was stabbed to death in Jerusalem, have said they are "devastated" by the "senseless
and tragic attack".Ms Bladon, 20, was attacked on a tram in Jerusalem
on Good Friday.She was studying at the Hebrew University of Jerusalem
at the time of her death and had been taking part in an archaeological
dig that morning.Ms Bladon was stabbed several times in the chest and
died in hospital. She was attacked by a man who pulled a knife from
his bag and repeatedly stabbed her on the tram travelling near Old
City, which was busy as Christians marked Good Friday and Jews
celebrated Passover.
I tried adding a space after certain punctuations like ".", "?", and "!".
article = article.replace(".", ". ")
It works with paragraphs (although I believe there should be a smarter way of doing this) but not with subtitles for different sections of the articles which don't have any punctuation in the end. They are structured like this:
</p>
<h2 class="story-body__crosshead">
Subtitle text
</h2>
<p>
I will be grateful for your advice.
PS: adding a space when I 'join' the article_soup doesn't help.
You can use separator in your get_text, which will fetch all the strings in the current element separated by the given character.
article_soup = [s.get_text(separator="\n", strip=True) for s in soup.find_all( 'div', {'class': 'story-body__inner'})]

Converting a textfile into a list

I have a text file which contains a series of movie titles, which looks like this once opened.
A Nous la Liberte (1932) About Schmidt (2002) Absence of Malice
(1981) Adam's Rib (1949) Adaptation (2002) The Adjuster (1991) The
Adventures of Robin Hood (1938) Affliction (1998) The African Queen
(1952)
Using the code below:
def movie_text():
moviefile = open("movies.txt", 'r')
yourResult = [line.split('\n') for line in moviefile.readlines()]
movie_text()
I get nothing.
Your code doesn't prints right.
If I understand it well,
moviefile = open("movies.txt", 'r')
lines=moviefile.readlines()
print(len(lines)) # Shows list size
for line in lines:
print(line[:1]) # The [:1] part cuts the \n
The method readlines returns a list, I am not sure why your use split. I mean, if all you want is to remove the '\n', you can do it in many ways, being the one I used just one of them.
Hope it works!

laTex cv currvita

I found this template and I would like to modify two things but all I tried does not work:
I would like to have more white vertical space before it displays my name since it's too close to the top as it is now
I would like the text of experience (blablablablabla..) to be more wide and therefore to reduce left and right margins
Any ideas on how to modify this template?
Thx
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Classicthesis-Styled CV
% LaTeX Template
% Version 1.0 (22/2/13)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% Original author:
% Alessandro Plasmati
%
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------------------------------------------------------
% PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------
\documentclass{scrartcl}
\reversemarginpar % Move the margin to the left of the page
\newcommand{\MarginText}[1]{\marginpar{\raggedleft\itshape\small#1}} % New command defining the margin text style
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,italian]{babel}
\usepackage[nochapters]{classicthesis} % Use the classicthesis style for the style of the document
\usepackage[LabelsAligned]{currvita} % Use the currvita style for the layout of the document
\renewcommand{\cvheadingfont}{\hspace{3.5cm}\LARGE\color{Maroon}} % Font color of your name at the top
\usepackage{hyperref} % Required for adding links and customizing them
\hypersetup{colorlinks, breaklinks, urlcolor=Maroon, linkcolor=Maroon} % Set link colors
\newlength{\datebox}\settowidth{\datebox}{Spring 2011} % Set the width of the date box in each block
\newcommand{\NewEntry}[3]{\noindent\hangindent=2em\hangafter=0 \parbox{\datebox}{\small \textit{#1}}\hspace{1.5em} #2 #3 % Define a command for each new block - change spacing and font sizes here: #1 is the left margin, #2 is the italic date field and #3 is the position/employer/location field
\vspace{0.3em}} % Add some white space after each new entry
%
\newcommand{\Description}[1]{\hangindent=1em\hangafter=0\noindent\raggedright\footnotesize{#1}\par\normalsize\vspace{1em}} % Define a command for descriptions of each entry - change spacing and font sizes here
%----------------------------------------------------------------------------------------
\date{} % Don't print the date
\begin{document}
\thispagestyle{empty} % Stop the page count at the bottom of the first page
%----------------------------------------------------------------------------------------
% CONTACT INFORMATION
%----------------------------------------------------------------------------------------
\begin{cv}{\spacedallcaps{Mario Rossi}}\vspace{1.8em} % Your name
\noindent\spacedlowsmallcaps{Contact Information}
\vspace{0.1em}
\hrule
\vspace{1em}
\NewEntry{Address}{Salita del carro, L'isola che non c'è} % Address
\NewEntry{Email}{\href{mailto:name#gmail.com}{name#gmail.com}} % Email address
\NewEntry{Linkedin}{\href{http://it.linkedin.com/pub/....}{http://it.linkedin.com/...../}} % Linkedin
\NewEntry{Phone}{+39 333\ \ $\cdotp$\ \ 11111111} % Phone number
%\vspace{1em} % Extra white space between the personal information section and goal
%\noindent\spacedlowsmallcaps{Goal}\vspace{1em} % Goal heading, could be used for a quotation or short profile instead
%\Description{Gain fundamental experience in my area of interest and expertise.}\vspace{2em} % Goal text
%----------------------------------------------------------------------------------------
% EXPERIENCE
%----------------------------------------------------------------------------------------
\vspace{0.6em}% Extra space between major sections
\noindent\spacedlowsmallcaps{Experience}
\vspace{0.1em}
\hrule
\vspace{1em}
%------------------------------------------------
\NewEntry{}{ \textsc{Somewhere,\textit{ City} }}
\Description{\MarginText{July - December 2015}blablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablabla\\ }
%----------------------------------------------------------------------------------------
\end{cv}
\end{document}
Here is how I addressed your two requirements:
Insert an invisible, vertical strut as part of the \cvheadingfont. I used \rule{0pt}{100pt}, but you can adjust (increase/decrease) the value of 100pt to move the content up/down.
Switched the document class to use the default article class, since there seems to be no need for using KOMA-script. This also allows for ease-of-use when changing the page layout/geometry using geometry. You can adjust the left and right margins to suit your needs.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Classicthesis-Styled CV
% LaTeX Template
% Version 1.0 (22/2/13)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% Original author:
% Alessandro Plasmati
%
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------------------------------------------------------
% PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------
\documentclass{article}
\reversemarginpar % Move the margin to the left of the page
\newcommand{\MarginText}[1]{\marginpar{\raggedleft\itshape\small#1}} % New command defining the margin text style
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,italian]{babel}
\usepackage[nochapters]{classicthesis} % Use the classicthesis style for the style of the document
\usepackage[LabelsAligned]{currvita} % Use the currvita style for the layout of the document
\usepackage{lipsum}
\renewcommand{\cvheadingfont}{%
\rule{0pt}{100pt}%
\centering\LARGE\color{Maroon}} % Font color of your name at the top
\usepackage{hyperref} % Required for adding links and customizing them
\hypersetup{colorlinks, breaklinks, urlcolor=Maroon, linkcolor=Maroon} % Set link colors
\newlength{\datebox}\settowidth{\datebox}{Spring 2011} % Set the width of the date box in each block
\newcommand{\NewEntry}[3]{%
\noindent\hangindent=2em\hangafter=0
\parbox{\datebox}{\small \textit{#1}}\hspace{1.5em} #2 #3 % Define a command for each new block - change spacing and font sizes here:
% #1 is the left margin,
% #2 is the italic date field and
% #3 is the position/employer/location field
\vspace{0.3em}} % Add some white space after each new entry
%
\newcommand{\Description}[1]{%
\hangindent=1em\hangafter=0
\noindent\raggedright\footnotesize #1\par
\normalsize\vspace{1em}} % Define a command for descriptions of each entry - change spacing and font sizes here
\usepackage[left=100pt,right=2cm]{geometry}
%----------------------------------------------------------------------------------------
\date{} % Don't print the date
\begin{document}
\thispagestyle{empty} % Stop the page count at the bottom of the first page
%----------------------------------------------------------------------------------------
% CONTACT INFORMATION
%----------------------------------------------------------------------------------------
\begin{cv}{\spacedallcaps{Mario Rossi}}\vspace{1.8em} % Your name
\noindent\spacedlowsmallcaps{Contact Information}
\vspace{0.1em}
\hrule
\vspace{1em}
\NewEntry{Address}{Salita del carro, L'isola che non c'è} % Address
\NewEntry{Email}{\href{mailto:name#gmail.com}{name#gmail.com}} % Email address
\NewEntry{Linkedin}{\href{http://it.linkedin.com/pub/....}{http://it.linkedin.com/...../}} % Linkedin
\NewEntry{Phone}{+39 333\ \ $\cdotp$\ \ 11111111} % Phone number
%\vspace{1em} % Extra white space between the personal information section and goal
%\noindent\spacedlowsmallcaps{Goal}\vspace{1em} % Goal heading, could be used for a quotation or short profile instead
%\Description{Gain fundamental experience in my area of interest and expertise.}\vspace{2em} % Goal text
%----------------------------------------------------------------------------------------
% EXPERIENCE
%----------------------------------------------------------------------------------------
\vspace{0.6em}% Extra space between major sections
\noindent\spacedlowsmallcaps{Experience}
\vspace{0.1em}
\hrule
\vspace{1em}
%------------------------------------------------
\NewEntry{}{\textsc{Somewhere,\textit{City}}}
\Description{\MarginText{July - December 2015}\lipsum[1]}
%----------------------------------------------------------------------------------------
\end{cv}
\end{document}