3

I'm trying to make flashcards to export to Anki, a flashcard program. I'm using the following link: http://reh.math.uni-duesseldorf.de/~zibrowius/LatexNoteImporter/

I'd like to assign each card with a unique identifier for the first field. This way, if I fix a typo on the question side, everything isn't all messed up in Anki.

What they did in the example was copy/paste a random number from a UUID generator. I don't want to have to copy and paste anything. It's also important that every time I re-compile or come back to the code, each card has the same ID as it did before.

I'm using TexStudio and I currently have a macro that sets up every other part of the flashcard. I'd like to make something that could be just added to the macro and I wouldn't have to type. Here's the current macro I'm using:

\begin{note}
    \xplain{id would go here}%add identifier
    \tags{%|%<tags%>%|}
    \begin{field}
        %<Question%>
    \end{field}
    \begin{field}
        %<Answer%>
    \end{field}
\end{note}

There are a couple ways I could think of doing this:

  • Create a list of numbers, kind of like \chapters. But the downside is, I don't actually need chapters, I just need the number in it. The other problem is that if I want to insert something out of order or delete a card, then everything down the line is messed up, and I'm sure it could all get dicey.
  • Generate random numbers. I see this other question for how to make a GUID, but I don't know how I could put that in a macro (which part is in my preamble/before? which part is in the macro? I just really don't understand what's going on in the code in the answer), or whether it would be the exact same each time I come back
  • A date/timestamp that would indicate the time at which I typed up this specific flashcard into existence, but wouldn't change when I compile things, and would also export to another computer (ie rely on being on my specific computer/flashdrive). The other thing is that I don't think if I specify say that it comes out a certain way in the PDF output that Anki would understand it

I'm sure there's probably other ways to do this too, and I wouldn't even be surprised if someone's already made something exactly like this!

For context: I'm not very experienced in LaTeX or programming, and a lot of this stuff is kind of confusing to me. For example, figuring out how to make macros took a while. So I really appreciate all of your inputs!!

The main motivation for using Anki with LaTeX is that I just really like using flashcards to learn definitions in math and find it effective. But typing LaTeX in Anki itself takes forever, and I prefer using an IDE which saves me so much time.

2

If the identifier doesn't have to be cryptic, I'd suggest to use a very simple function that maps the date where you start a card to an ID, in an injective way (in this message, the word “date” really means date + time, i.e.: something precise enough to distinguish each card). This ensures that a card always has the same ID and that any two cards started at different times have different IDs. Here is a simple implementation of this scheme:

\documentclass{article}

\makeatletter
\def\@cardStartedOn#1-#2-#3 #4:#5\@nil{%
  \gdef\@cardId{#1#2#3#4#5}%
}

\newcommand*{\cardStartedOn}[1]{\@cardStartedOn #1\@nil}
\newcommand*{\cardId}{\@cardId}
\makeatother

\cardStartedOn{2020-02-02 12:44}

\begin{document}

The identifier of this card is \cardId. Let me repeat: \cardId\ is the
identifier of this card. :-)

\end{document}

screenshot

Two possible changes:

  • In case : is an active character in your language (this can be the case with \usepackage[french]{babel}, depending on the TeX engine used), one can either choose a different input delimiter between hours and minutes, or modify the code to match against the active :.

  • If you need the ID to be cryptic, one could extend this by “simply” computing a hash from the date (of course, it is easy to add seconds if really necessary). This could be done for instance using a well-known algorithm such as SHA1, or even reusing a GUID function from the question you linked to: you could pass the number-date (such as 202002021244) as the seed used by the random number generator, and would use the first generated value as your card identifier.

The acceptable variants for cryptic IDs of course depend on the acceptable format for card IDs according to the platform you are using.

| improve this answer | |
  • Hi @frougon - thank you so much for your response! It doesn't have to be cryptic at all. I was wondering- am I supposed to type in like 2020-02-02 12:44, or is that supposed to automatically come up? – electriclady Feb 2 at 12:38
  • Then you should be ready to go. :-) – frougon Feb 2 at 12:38
  • Oops I'm sorry I think I edited right after you commented- do you know if I'm supposed to type in 2020-02-02 12:44 (for example), or is that supposed to automatically come up? – electriclady Feb 2 at 13:03
  • You need to type it yourself, once for each card. This is about the simplest way to guarantee that the same card will keep its identifier, even if you change its contents. Otherwise, one could write the current date to an external file and test its presence when initializing, but it's a bit more complicated and you would need to carry the external file and the .tex file to other computers in order to keep the ID unchanged. – frougon Feb 2 at 13:36
  • @frougon If I got the question right, at some stage the .tex-source will not be processed by some LaTeX-compiler but will be processed by the software "LaTeX Note Importer for Anki". The question is: Will "LaTeX Note Importer for Anki" execute (La)TeX-macros at all? Probably the OP wishes an editor-macro for TeXStudio which helps maintaining unique-ids and inserting note-environments with proper ids into the .tex-source-file automatically. – Ulrich Diez Feb 2 at 15:28
2

You say you use TeXStudio while TeXStudio is editing software for creating and maintaining .tex-source-files. You wish to have a macro for maintaining unique ids in terms of \xplain{...}-entries in note-environments.

So the question arises:

Do you wish the macro to be an editor-macro which is part of your configuration of TeXStudio and which is available when operating the software TeXStudio?

Do you wish the macro to be a macro from the (La)TeX-programming-language which will be carried out when the LaTeX-compiler is compiling your .tex-files?

Let's abstract the problem:

The single instances of the note-environment form records of a database maintained in terms of your .tex-source-files.

You wish a mechanism for automatically obtaining primary keys (unique identifiers) for these records so records of the database can be identified by means of these primary keys.

Basically there are (at least) two possibilities for dealing with primary keys/unique ids in databases:

Possibility 1:

Either with each record of the database some of its elements can be combined in a way in which the combination of these elements already forms a unique primary key which can be used for deriving the record's unique identifier.
Sometimes this is possible. Often this is not possible.

A variation of possibility 1 can be that the whole record itself forms a primary key calculable by taking the set of characters that forms the record and calculating some hash-value by means of a perfect hash-function.

With possibility 1 the primary key/the unique identifier of a record of the database changes as soon as some of those components of the record get changed from which the primary key/unique identifier is derived.

Seems at some stage the .tex-input-files are not to be processed by whatsoever LaTeX compiler but by the software "LaTeX Note Importer for Anki". I doubt that the "infrastructure" provided by the software "LaTeX Note Importer for Anki" is suitable for post-processing the content of note-environments in a way where it is possible to extract pieces of data in order to somehow derive primary keys/unique ids from them.

Therefore seems possibility 1 is not really an option.

Possibility 2:

Or each record of the database needs to have an element for storing the record's unique ID, which is to be generated somehow. E.g., by counting records or (in case of not creating two records at the same time) by using some sort of time-stamp from the moment of creating the record, or whatever. Then this generated element is the primary key for identifying the record. In this case this element is a component of the database and thus needs to be stored as such.

Adding a primary-key-element to a database-entry/record and permanently storing it requires modifying the set of files which makes up the database in question.

Database-management programs like MySQL, MariaDB or Microsoft Access are designed for automatically generating and maintaining primary keys/unique identifiers and modifying database-files=the source-files that form the database in question for storing these primary keys.

As you use .tex-source-files for maintaining your database-records/your database-entries in terms of note-environments, you need mechanisms for creating primary keys/unique ids and for changing/editing those of your .tex-source-files that make up your note-database in order to store these primary keys/unique ids.

One mechanism could be formed by you and your favourite .tex editor when you type the unique id belonging to an instance of the note-environment yourself. ;-)

Probably this can be automatized by implementing some editor-macros for TeXStudio (not macros in the programming language TeX/LaTeX) for creating/maintaining unique-ids and inserting note-environments with these unique-ids into your .tex-source-files automatically.

I seldom use TeXStudio.

In case it is ensured that only one note-environment is generated at a time, I recommend following frougon's suggestion of using time-stamps of that moment when the instance of the note-environment is inserted into the .tex-source-file. (For the sake of readability that time-stamp can probably be in some ISO8601-format).
I suggest implementing a TeXStudio-editor-macro which inserts an instance of the note-environment into your .tex-source-file where the \xplain-command is of pattern:

\xplain{ID=(⟨year⟩-⟨month⟩-⟨day⟩T⟨hour⟩:⟨minute⟩:⟨second⟩⟨UTC-time-zone-designator⟩)}

If the unique id is formed from a time-stamp of the moment when the instance of the note-environment gets inserted into your .tex-source-file, you don't need to maintain lists of unique-ids.
But you need to ensure that the clock of your system works correctly whenever having TeXStudio insert another instance of the note-environment!

In case such time-stamps for some reason are not suitable as unique-ids:

In the past I used WinEdt. Back then WinEdt had nice editor-macros for maintaining referencing labels by parsing .tex-input-files and displaying a list with the referencing-labels that can be found as arguments of \label-commands. Back then the mechanism was not perfect as referencing-labels coming from other files via \input/\include/xr(-hyper)-package's \externaldocument were not taken into consideration.

Probably a similar feature exists in TeXStudio and can be adapted to\xplain-commands instead of \label-commands and can be enhanced to suggest new strings which are not already in the list.

| 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.