8000 Feature request: environment that expands LaTeX macros before writing python code · Issue #214 · gpoore/pythontex · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Feature request: environment that expands LaTeX macros before writing python code #214
Open
@matterhorn103

Description

@matterhorn103

Hi!

I am basically trying to do the same thing as the user on SE here:
https://tex.stackexchange.com/questions/515463/pythontex-using-latex-macros-in-pyblock?newreg=08b9004544364c2683c20a8737bc7586
i.e. pass the output of a LaTeX macro to the python side.

You say in your response on SE that it would be possible to make an environment that expands macros and passes the results to the python code. It is also listed as a possible future development in the documentation. Did you ever look into it? Is it possible that something like that could be implemented?

Using \pyc like in your comment also doesn't work for me.

I also assume that implementation of a new environment like this would require TeX coding rather than python coding, and would thus not be something I could implement myself?

Many thanks.


Purely for the sake of completeness, some info on what I am trying to do:

I am using the package chemnum, which generates a list of key:value pairs. The value ("label") for a key ("id") can be accessed within LaTeX using the macro \cmpd{key}, but I want to pass the pairs to pythontex, manipulate them, and then generate LaTeX code after manipulation. Specifically, I want to order the keys/IDs in ascending order of their values/labels. I think this would be a challenge in TeX, and I certainly don't know how to do it - but I do know how I would do it in python. To do this I would use the following code (of which the first block is the relevant one):

\begin{pycode}
dict1 = {}
for item in some_list:
    dict1[item] = \cmpd{item}
\end{pycode}

\begin{pycode}
dict2 = {value: key for key, value in dict1.items()}
for label in range(500):
    if label in dict2:
        print(fr"\input{{chapters/cmpds/{dict2[label]}}}")
\end{pycode}

So if the key value pairs are {"216": 2, "450": 1, "378": 3} output would be for example:

\input{chapters/cmpds/450}
\input{chapters/cmpds/216}
\input{chapters/cmpds/378}

It seems this is not possible currently with pythontex. In the linked StackExchange question you suggest that the one-line pythontex structures do indeed expand macros but even if I replace my first pycode block with the suggested code and remove the loop it still doesn't work:

\pyc{dict1["450"] = """\cmpd{450}"""

Instead, the value in the dictionary for the key "450" is just "\cmpd{450}", not the result of that macro, which would be 2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0