1

I want to format a string using variables. I am aware of the % and the .format() options.

However, as the string I want to format is a piece of LaTeX code, I chose to use the outdated %-version since there are already { and } characters in my code. (And I don't want to escape them because the code will be frequently updated).

My issue is : when I run my code, I get this error :

TypeError: not enough arguments for format string

I use a dictionary to define the variables, and they are all defined. As far as I understood it, it might come from the % character used for comments in LaTeX and that could be mis-interpreted in python.

How could I manage this?

Community
  • 1
  • 1
ebosi
  • 1,285
  • 5
  • 17
  • 37
  • 1
    Please isolate the problem so you can show us some code. As it stands, I wonder if you're maybe not using `%(key)s` to get the values from your dict? – Ulrich Schwarz Oct 11 '15 at 15:27
  • @Ulrich I'll clean up my code and post it. To answer your question, I am using `%(key)s` in my `.tex` code and defined a `dict = {'key1' = "foo", 'key2' = "bar"}`. (-: – ebosi Oct 11 '15 at 15:30
  • Maybe you've forgotten a `s` somewhere, happens to me all the time, because `%` would give a `KeyError` if it can't find a key from the dict, the `TypeError` you give should only appear if you use the non-dict version and have more `%`s than replacements. – Ulrich Schwarz Oct 11 '15 at 15:33
  • @Ulrich got it! see my auto-answer. sorry for disturbing, and thx for answer (-: – ebosi Oct 11 '15 at 15:56

1 Answers1

0

The real problem/solution was : my .tex code contained following commented TeX command : %\begin{itemize} and then %\end{itemize}.

Python understood the %\-pattern as a a call for a key... what wasn't intended.

Nota: in order to make your LaTeX code python-readable, you need to escape your %, i.e. comment your .tex code with this pattern : %% (%%Space)

ebosi
  • 1,285
  • 5
  • 17
  • 37