1

I am trying to compile a bilingual dictionary from a spreadsheet file that was exported as csv. The file contains columns with the kinds of information that one would expect from a dictionary: lexeme, part of speech, gender, sense, example sentence, example translation. I am simplifying things here for a better explanation.

lexeme,partofspeech,gender,sense,example,extranslation

For my tex-file I used \newcommand to create an \entry command with six arguments. In this way, I can define the formatting (parentheses, italics, headers, etc.)

\newcommand{\entry}[6]{\markboth{#1}{#1}{\textbf{#1}}\ {\textcolor{blue}{(#2)}}\ {#3}\ $\bullet$\ {#4} \ {\textit{#5}} {#6}\vspace{0.2cm}\par}

In the document, I call \entry when importing the csv-file. I used the csvsimple package to import my csv-file. Following \csvreader: the {first argument} identifies the csv-file, the {second argument} defines my table headings, the {third argument} contains the \entry command that is applied to each line in the csv.

\csvreader{dictionary.csv}{lexeme=\lexeme,partofspeech=\partofspeech,gender=\gender,sense=\sense,example=\example,extranslation=\extranslation}{\entry{\lexeme}{\partofspeech}{\gender}{\sense}{\example}{\extranslation}}

All this works fine for a csv-file that looks like this:

lexeme,partofspeech,gender,sense,example,extranslation
lexeme-1,noun,feminine,translation-1,example-1,extranslation-1
lexeme-2,noun,masculine,translation-2,example-2,extranslation-2

However, there are lexemes/entries that contain more than one example sentence. And they looks like this:

lexeme,partofspeech,gender,sense,example,extranslation
lexeme-1,noun,feminine,translation-1,example-1a,extranslation-1a
,,,,example-1b,extranslation-1b

Moreover, there are lexemes/entries that have more than one sense. and they would look like this:

lexeme,partofspeech,gender,sense,example,extranslation
lexeme-1,noun,feminine,translation-1a,example-1a,extranslation-1a
,verb,,translation-1b,example-1b,translation-1b,

With the \csvreader settings (above), I get problems. As one would expect, I get a new entry for each new line, when I really to add just an example/translation that belongs to the lexemes/entry in previous line. This would be relevant for all lines in which the first value is empty (i.e. that to not have a lexeme). How can I do this?

EDIT: I was asked to provide a MWE

\documentclass[11pt,a4paper,twoside]{article}
\usepackage{fontspec}
\usepackage{csvsimple}
\usepackage{color}
\newcommand{\entry}[6]{\markboth{#1}{#1}{\textbf{#1}}\ {\textcolor{blue}{(#2)}}\ {#3}\ $\bullet$\ {#4} \ {\textit{#5}} {#6}\vspace{0.2cm}\par}
\begin{document}
    \csvreader{dictionary.csv}{lexeme=\lexeme,partofspeech=\partofspeech,gender=\gender,sense=\sense,example=\example,extranslation=\extranslation}{\entry{\lexeme}{\partofspeech}{\gender}{\sense}{\example}{\extranslation}}
\end{document}

The csv-file look like this:

lexeme,partofspeech,gender,sense,example,extranslation
lexeme-1,noun,feminine,translation-1a,example-1a,extranslation-1a
,verb,,translation-1b,example-1b,translation-1b
lexeme-2,noun,feminine,translation-2,example-2a,extranslation-2a
,,,,example-2b,extranslation-2b

The pdf-output of the MWE contains four dictionary entries. What I want to achieve is that lines two and four from the csv-file are appended to the respective cells (i.e. in the same column) of the preceding line.

  • Hello, can you provide a compilable MWE? – Sango Feb 4 at 13:57
  • 1
    you can detect the first entry is empty with (for example) \ifx\relax#1\relax empty \else ... \fi but what you can do in the empty case depends on how you have typeset the previous row and whether you can easily add this extra information, so a more complete example would help – David Carlisle Feb 4 at 14:12
  • Also, if it is viable for you, you should consider using different programming tool to "normalize" your csv into form, that is only read with csvsimple; e.g. use python or R to create "normalized" csv. – Tomáš Kruliš Feb 4 at 14:48
  • @DavidCarlisle: Thank you for your hint. The \if-\else approach is great. But how should can "I reach back to the previous line" in case the if-condition has been satisfied? Could you suggest something to read on this topic? Sorry, about my naive questions. I feel like I am diving in the deep end. – moorooduc Feb 4 at 16:07
  • as I say that depends on what you are doing with each line, it may be easy or verging on impossible to "go back" to the previous line, depending on what you did with it. . In this case I would remove \vspace{0.2cm}\par (\vspace before the par is wrong anyway) and then just add \par at the start of the entry in the non-empty case, – David Carlisle Feb 4 at 16:11
1

You can test for the setting you need, and only start a new paragraph at the start of really new entries. enter image description here

\documentclass[11pt,a4paper,twoside]{article}
\usepackage{fontspec}
\usepackage{csvsimple}
\usepackage{color}
\newcommand{\entry}[6]{%
\if$#1$\else\markboth{#1}{#1}\par\textbf{#1}\ {\textcolor{blue}{(#2)}}\ {#3}\ $\bullet$\ {#4} \fi
\ \textit{#5} {#6}%
}
\begin{document}
\setlength\parskip{.5ex}
    \csvreader{dictionary.csv}{
lexeme=\lexeme,
partofspeech=\partofspeech,
gender=\gender,sense=\sense,
example=\example,
extranslation=\extranslation}{%
\entry{\lexeme}{\partofspeech}{\gender}{\sense}{\example}{\extranslation}}
\end{document}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.