Skip to content

Improved latex formatting #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 68 additions & 11 deletions gca/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ def format_affiliation(self):
active = filter(bool, components)
return u', '.join(active)

def latex_format_affiliation(self):
department = self._data['department']
section = self._data['section']
address = self._data['address']
country = self._data['country']
components = [u', ' + c if c and len(c) else u'' for c in [department, section, address, country]]
return u'\\affiliation{' + '}{'.join(components) + '}'


class Author(Entity):
def __init__(self, data=None):
Expand Down Expand Up @@ -220,28 +228,35 @@ def full_name(self):
def format_name(self, inverted=False):
d = self._data

first = self.sanitize_name(d['firstName'])

if inverted:
middle = self.format_initials(d['middleName'], suffix='.')
if middle and len(middle):
middle = ' ' + middle
return "%s, %s%s" % (d['lastName'], d['firstName'], middle)
middle = u' ' + middle
return "%s, %s%s" % (d['lastName'], first, middle)

middle = d['middleName'] + u' ' if d['middleName'] else u""
return d['firstName'] + u' ' + middle + d['lastName']
middle = self.sanitize_name(d['middleName'])
middle = middle + u' ' if middle else u''
return first + u' ' + middle + d['lastName']

def latex_format_name(self):
d = self._data
first = ''
shortfirst = ''
if d['firstName'] and len(d['firstName']):
first = d['firstName']
shortfirst = self.format_initials(d['firstName'], suffix='.')
first = self.sanitize_name(d['firstName'], separator=' ', suffix='.')
shortfirst = self.format_initials(d['firstName'],
separator=' ', suffix='.')
middle = ''
shortmiddle = ''
if d['middleName'] and len(d['middleName']):
middle = u' ' + d['middleName']
shortmiddle = u' ' + self.format_initials(d['middleName'], suffix='.')
return u"\\authorname{%s}{%s}{%s}{%s}{%s}" % (first, middle, shortfirst, shortmiddle, d['lastName'])
middle = u' ' + self.sanitize_name(d['middleName'], separator=' ',
suffix='.')
shortmiddle = u' ' + self.format_initials(d['middleName'],
separator=' ',
suffix='.')
return u"\\authorname{%s}{%s}{%s}{%s}{%s}" % (first, middle, shortfirst, shortmiddle, d['lastName'].strip())

def format_affiliation(self):
af = self._data['affiliations']
Expand All @@ -254,12 +269,39 @@ def index_name(self):
self.format_initials(self.first_name),
self.format_initials(self.middle_name))

@staticmethod
def sanitize_name(name, separator='', suffix=''):
""" Apply to first and middle name to separate initials.
E.g. 'MS' -> 'M. S.'
E.g. 'M' -> 'M.'
"""
if not name:
return ""
# make sure name is split after every '.' and around '-':
name = name.strip().replace('.', '. ')
name = name.replace('-', ' - ')
# split into components and also split double and triple initials (e.g. 'HJ'):
comps = [y for x in name.split() for y in (x if len(x)<=3 and x.isupper() and x.isalpha() else [x] )]
text = separator.join([a if len(a) > 1 else a[0] + suffix if a[0] != '-' else a[0] for a in comps])
if separator:
return text.replace(separator+'-'+separator, '-')
else:
return text

@staticmethod
def format_initials(name, separator='', suffix=''):
if not name:
return ""
comps = name.split(' ')
return separator.join([a[0] + suffix for a in comps])
# make sure name is split after every '.' and around '-':
name = name.strip().replace('.', '. ')
name = name.replace('-', ' - ')
# split into components and also split double and triple initials (e.g. 'HJ'):
comps = [y for x in name.split() for y in (x if len(x)<=3 and x.isupper() and x.isalpha() else [x] )]
text = separator.join([a[0] + suffix if a[0] != '-' else a[0] for a in comps])
if separator:
return text.replace(separator+'-'+separator, '-')
else:
return text


class Reference(Entity):
Expand Down Expand Up @@ -745,3 +787,18 @@ def to_json(data):
js = json.dumps(data, sort_keys=True, indent=4,
separators=(',', ': '), ensure_ascii=False)
return js


if __name__ == "__main__":
# test author formatting:
names = [' Hans ', 'Hans', 'H.', 'H',
' Hans Juergen ', 'Hans Juergen', 'H. J.', ' H. J. ', 'H.J.', 'H.J', 'HJ',
' Hans - Juergen ', 'Hans-Juergen', 'H.-J.', ' H. - J. ', 'H.-J.', 'H-J',
' Peter Hans - Juergen ', 'Peter Hans-Juergen', 'P. H.-J.', 'P. H. - J. ', 'P.H.-J.', 'P H-J', 'PH-J', 'PETER', 'PET', 'Pet', 'PETE']
print('sanitize_name():')
for n in names:
print('%-25s: %s' % (n, Author.sanitize_name(n, separator=' ', suffix='.')))
print()
print('format_initials():')
for n in names:
print('%-25s: %s' % (n, Author.format_initials(n, separator=' ', suffix='.')))
44 changes: 28 additions & 16 deletions gca/tex.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,34 +154,42 @@ def check_cur_state(abstract, state):
%endif

%% Command for setting the abstract's title. It gets four arguments:
%% 1. some optional string (user supplied by manual editing the latex file)
%% 2. poster ID
%% 3. last name of first author
%% 4. title of abstract
\newcommand{\abstracttitle}[4][]{\section[#3: #4]{#4}}
%%\newcommand{\abstracttitle}[4][]{\section[#2]{#4}}
%% 1. poster ID
%% 2. last name of first author
%% 3. title of abstract
\newcommand{\abstracttitle}[3]{\section[#2: #3]{#3}}
%%\newcommand{\abstracttitle}[3]{\section[#1]{#3}}

%% Environment for formatting the authors block:
\newenvironment{authors}
{\begin{flushleft}\setstretch{1.2}\sffamily}
{\end{flushleft}\vspace{-3ex}}

%% Command for formatting of author names. Five arguments:
%% Command for formatting author names. Five arguments:
%% 1. first name
%% 2. middle name
%% 3. initial of first name
%% 4. initial of middle name
%% 3. initials of first name
%% 4. initials of middle name
%% 5. last name
\newcommand{\authorname}[5]{\mbox{#1#2 \textbf{#5}}} %% first and middle name plus bold last name
\newcommand{\authorname}[5]{\mbox{#1#2 \textbf{#5}}} %% full first and middle name plus bold last name
%% \newcommand{\authorname}[5]{\mbox{\textbf{#5}, #3#4}} %% bold last name, first and middle initials
%% \newcommand{\authorname}[5]{\mbox{\textbf{#5}, #1#4}} %% bold last name, full first name and middle initials

%% Environment for formatting the affiliations:
%% Environment for formatting affiliations:
%% each affiliation is provided as an \item
\newenvironment{affiliations}
{\begin{flushleft}\begin{enumerate}\setlength{\itemsep}{-0.5ex}\footnotesize\sffamily}
{\end{enumerate}\end{flushleft}}

\usepackage{xstring}
%% Command for formatting each affiliation. Four arguments:
%% 1. department
%% 2. section
%% 3. address
%% 4. country
%% Each of the arguments are either empty strings or preceded by ', '.
\newcommand{\affiliation}[4]{\StrGobbleLeft{#1#2#3#4}{2}}

%% Environment for formatting the abstract's main text:
\newenvironment{abstracttext}
{\noindent\hspace*{-0.8ex}}
Expand All @@ -194,7 +202,11 @@ def check_cur_state(abstract, state):

%% Maximum height of a figure:
\newlength{\figureheight}
\setlength{\figureheight}{0.35\textheight}
\setlength{\figureheight}{0.3\textheight}

%% Command for including an image:
\newcommand{\includeimage}[1]
{\centerline{\includegraphics[width=1\linewidth, height=1\figureheight, keepaspectratio]{#1}}}

%% Environment for formatting the acknowledgements block:
\newenvironment{acknowledgements}
Expand Down Expand Up @@ -234,9 +246,9 @@ def check_cur_state(abstract, state):

${mk_abstract(idx, abstract, figures is not None, show_meta)}
% endfor
%if not bare:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% appendix
%if not bare:
\backmatter

\printindex[authors]
Expand All @@ -247,7 +259,7 @@ def check_cur_state(abstract, state):

<%def name="mk_abstract(idx, abstract, include_figures, print_meta)">
\begin{abstractblock}
\abstracttitle[]{${abstract.poster_id}}{${abstract.authors[0].last_name}}{${mk_tex_text(abstract.title)}}
\abstracttitle{${abstract.poster_id}}{${abstract.authors[0].last_name}}{${mk_tex_text(abstract.title).rstrip('.')}}
${mk_authors(abstract.authors)}
${mk_affiliations(abstract.affiliations)}
%if abstract.doi:
Expand Down Expand Up @@ -297,7 +309,7 @@ def check_cur_state(abstract, state):
<%def name="mk_affiliations(affiliations)">
\begin{affiliations}
% for idx, affiliation in enumerate(affiliations):
\item[${idx+1}.] ${mk_tex_text(affiliation.format_affiliation())}
\item[${idx+1}.] ${mk_tex_text(affiliation.latex_format_affiliation())}
% endfor
\end{affiliations}
</%def>
Expand All @@ -318,7 +330,7 @@ def check_cur_state(abstract, state):

<%def name="mk_figure(figure)">
\begin{afigure}
\centerline{\includegraphics[width=1\linewidth, height=1\figureheight, keepaspectratio]{${figure.uuid}}}
\includeimage{${figure.uuid}}
\captionof*{figure}{\small ${mk_tex_text(figure.caption)}}
\end{afigure}
</%def>
Expand Down