Export Python variables and import them into your LaTeX document -- Combining Python with LaTeX

Поділитися
Вставка
  • Опубліковано 21 гру 2024

КОМЕНТАРІ • 51

  • @davidnatanael4561
    @davidnatanael4561 3 роки тому +3

    Great tutorial, clear explanation & easy to follow. Thanks for your video

  • @giovannyvelasquez9543
    @giovannyvelasquez9543 3 роки тому

    It was really helpful. I am now implementing these tools in my research's package. Definitely It will save a lot of time. Thank you Federico!

    • @FedericoTartarini
      @FedericoTartarini  3 роки тому

      I am glad you found it useful and thank you so much for writing a comment. If you have time, please have a look at my LaTeX playlist on UA-cam since I have many other videos you may find useful. Like exporting a Pandas dataframe to a LaTeX table.

  • @landisvinchattle8306
    @landisvinchattle8306 Рік тому

    Nice video. Looking at generating some dynamic formatted images using LaTeX and I'm new to it, this was a quick watch with a lot of good info. Thanks!

  • @Erith77
    @Erith77 3 роки тому +1

    Nice contribution. Thanks for the tutorial

  • @bryancarrillorojas9976
    @bryancarrillorojas9976 2 роки тому

    Wonderful video, it was just what I needed

  • @dskevinperezgarcia
    @dskevinperezgarcia 2 роки тому

    Gracias! Se puede implementar esto en Overleaf?

    • @FedericoTartarini
      @FedericoTartarini  2 роки тому +1

      Yes, you can kind of do that by connecting overleaf with GitHub. I have a video on that if you want to learn how to sync the two

  • @CastingSpell
    @CastingSpell Рік тому

    Hello there thanks for the video, its being so usefull, however I have two questions, If im running a project with multiple .tex files, once \var is defined on the main.tex, I get Undefined Control Secuence on the others, but somehow, it still works somethimes (yeah, fails in some cases), is there a need to remember the current .tex (sections/template.tex) the existance of \var?
    And the other one, can we use it to load images from it? I mean, save in a variable a image path, and then pass it to \includegraphics? For the tests I made, I didnt succeed, \var{img_path} remains green, like if \includegraphics treated it as a hole string, not as a variable.
    Could you please help me out, thanks!

  • @edwinpinerosmonroy9015
    @edwinpinerosmonroy9015 2 роки тому

    Thanks Federico,this video is very useful.
    But I have a question, if I have a graph that I generate with python, can I export it to the LaTex document?

    • @FedericoTartarini
      @FedericoTartarini  2 роки тому

      Yes, of course. You can save it as a PNG and import it into python

  • @Donotuselz
    @Donotuselz 2 роки тому

    Thanks Federico, this is very useful. Is there any way to do the same within a jupyter notebook?Thanks

    • @FedericoTartarini
      @FedericoTartarini  2 роки тому

      Sure you can do that in a Jupiter notebook as long as the notebook can save a file in your computer.

  • @sigurdvingelsgaard9466
    @sigurdvingelsgaard9466 3 роки тому

    Hi. I want to show my code with colors etc like in a code editor in my latex doc. The code should not run or do anythong in the document, just stand like in a code editor. Thanks

    • @FedericoTartarini
      @FedericoTartarini  3 роки тому

      Hi, you can follow this guide: www.overleaf.com/learn/latex/Code_listing.
      I am planning to release a video about this topic soon.

  • @Erith77
    @Erith77 3 роки тому +1

    Hi Federico, thank you very much for your tutorial. It has been very useful to me. However, I have a litter problem when I try to use the \var{key} entity inside a table. Can you help me to make work a table built from several \var{key} entities in its fields? thank in advance for any help

  • @JuanMCarini
    @JuanMCarini 2 роки тому

    Hi!! What app are you using for compile all this?

    • @FedericoTartarini
      @FedericoTartarini  2 роки тому +1

      I use pycharm. I have a video on how to get started with pycharm and latex, and another video in which I explain how to setup pycharm to work with python

    • @JuanMCarini
      @JuanMCarini 2 роки тому +1

      @@FedericoTartarini Thank you! You are my new idol!!

    • @FedericoTartarini
      @FedericoTartarini  2 роки тому

      Thank you so much. Let me know if you have any other questions and please check my LaTeX playlist. I am sure you will find some of the videos useful.

  • @filipefiedler
    @filipefiedler 2 роки тому

    Thanks a lot for the tutorial. It worked totally fine. However, I tried to use the var command inside a for loop using the pgffor package and it didn't work. I think it has something to do with DTLloaddb returning a local variable and not a global one, but I could not figure out how to change the code to make everything global. I would be very happy if you have any tips. Thanks again!

    • @FedericoTartarini
      @FedericoTartarini  2 роки тому +1

      Hi, unfortunately I have never tried to use it inside a for loop. What are you trying to do? Maybe there is another solution to solve the problem.

    • @filipefiedler
      @filipefiedler 2 роки тому

      ​@@FedericoTartarini Thanks for trying to help! I'm doing a for loop inside latex to insert some images and I need that the caption of each image states some data generated by my python code. As a final solution, I can abadon the for loop in latex and write all the figure environments using python, but I think a solution inside Latex would be better

    • @FedericoTartarini
      @FedericoTartarini  2 роки тому +1

      Yes, I think that it would be easier if you create a .tex file with python that imports the figure and the captions. Then in latex you can just import that file using the \input{} command. At this stage I would not know if you can solve the problem you are facing with the for loop. Sorry for that.

  • @joelvasiliev
    @joelvasiliev 3 роки тому

    great tutorial. it helped me so much
    thank you!!

    • @FedericoTartarini
      @FedericoTartarini  3 роки тому

      Thank you for letting me know. I am glad you found it useful.

  • @vladimiru8602
    @vladimiru8602 2 роки тому

    Great tutorial, thank you!
    But what do you think about running python inside LaTex with PythonTeX or PyLuaTeX? Wouldn't it be even better, and wouldn't it give even more features?

    • @FedericoTartarini
      @FedericoTartarini  2 роки тому +1

      Thank you very much. I have never tried those packages. I'll look into it.

    • @FedericoTartarini
      @FedericoTartarini  2 роки тому +1

      I am looking into PythonTeX and in general, unless you are teaching, I am against embedding Python code into LaTeX since that can make your project very messy. I prefer to have my python and latex separate.

  • @h2ocombolhas1
    @h2ocombolhas1 3 роки тому +1

    Very helpfull :)

  • @patrickstaudt2584
    @patrickstaudt2584 2 роки тому

    Hi, Dr. Tartarini. Thanks for making this video! I'm using overleaf to compile and getting a few errors. The first is "Package filecontents Warning: This package is obsolete. Disabling it and passing control to the filecontents environment defined by the LaTeX kernel." The second is "Runaway argument?" on line 120. Line 119 is "
    ewcommand{\testingtesting}[1]{\DTLfetch{mydata}{thekey}{#1}{thevalue}}", and line 120 is a blank line after 119. I suspect that passing control to the filecontents environment defined by the LaTeX kernel is causing the compiler to not handle line 119 properly. In any case, do you have advice? Thanks again!
    Edit: I figured it out! mydata.dat had a "maxdiff,0.92%" in it. the "%" was causing issues. Thanks again for the video!

    • @patrickstaudt2584
      @patrickstaudt2584 2 роки тому

      Actually, I figured it out! mydata.dat had a "maxdiff,0.92%" in it. the "%" was causing issues.

    • @FedericoTartarini
      @FedericoTartarini  2 роки тому

      Great that you were able to solve all the problems

  • @yodwan1
    @yodwan1 3 роки тому

    Thank you indeed.

  • @ЕгорГордеев-ъ3ф
    @ЕгорГордеев-ъ3ф 3 роки тому

    Thanks a lot for video! Is it real to make it in Word? Create document with data as result of working my program and export variables to Word document. And when I will change my program and get new data, variables automatically will change in document.

    • @FedericoTartarini
      @FedericoTartarini  3 роки тому

      Hi, I am glad you liked the video. Thank you for leaving a comment. I am sorry but I did not understand your question

  • @jordanseddon3908
    @jordanseddon3908 3 роки тому

    Hi I'm trying to write a python variable in the format #,### to the mydata.dat file, however I think there is an issue with the separator in the format. Can you help with a work around?

    • @FedericoTartarini
      @FedericoTartarini  3 роки тому

      Could you please share more information about the error. Have you followed all the steps as shown in the video?

    • @jordanseddon3908
      @jordanseddon3908 3 роки тому

      @@FedericoTartariniI have followed all the steps.
      I have defined the save_var_latex function as you did and have tried the following lines:
      save_var_latex('test_val1', '1234')
      save_var_latex('test_val2', '6789')
      These both work fine and when i check the newly created mydata file i can see both entries.
      However, when I run the following lines:
      save_var_latex('test_val3', '1,234')
      save_var_latex('test_val4, '6,789')
      I have an issue. Opening up the mydata file I can see the keys but the values haven't been written correctly, I'm assuming it doesn't like the "," separator in the numbers 1234 & 6789.
      I have tried to use a different delimiter, but I still have issues, the datafile now shows the following:
      test_val3|1|234
      test_val4|6,789
      Which is not what I was expecting.
      Appreciate any help you can give.

    • @FedericoTartarini
      @FedericoTartarini  3 роки тому +1

      Hi Jordan, thank you for sharing more info on this. Yes, the issue is caused by the fact that there is a comma in your numbers, this causes an issue with both the separator and the fact that inputs in functions are separated by a comma. You can fix the problem by using the following code, which uses | as a separator. Please note that you will have to pass the number into the Python function as a string.
      ```python
      def save_var_latex(key, value):
      import csv
      import os
      dict_var = {}
      file_path = os.path.join(os.getcwd(), "mydata.dat")
      try:
      with open(file_path, newline="") as file:
      reader = csv.reader(file, delimiter ='|')
      for row in reader:
      dict_var[row[0]] = row[1]
      except FileNotFoundError:
      pass
      dict_var[key] = value
      with open(file_path, "w") as f:
      for key in dict_var.keys():
      f.write(f"{key}|{dict_var[key]}
      ")
      save_var_latex("x", "6,38")
      ```
      ```latex
      % package to open file containing variables
      \usepackage{datatool, filecontents}
      \DTLsetseparator{|}% Set the separator between the columns.
      % import data
      \DTLloaddb[noheader, keys={thekey,thevalue}]{mydata}{../mydata.dat}
      % Loads mydata.dat with column headers 'thekey' and 'thevalue'

      ewcommand{\var}[1]{\DTLfetch{mydata}{thekey}{#1}{thevalue}}
      ```

    • @jordanseddon3908
      @jordanseddon3908 3 роки тому

      @@FedericoTartarini Perfect that works! I was so close with my attempt to correct this - I had missed the ", delimiter = "|" " part in line 10.

  • @ivanssmirnoff
    @ivanssmirnoff 3 роки тому

    Thanks a lot for the video amico

    • @FedericoTartarini
      @FedericoTartarini  3 роки тому +1

      Thank you so much for letting me know and for writing a comment. I am glad that you found my video useful. If you can, please share the video with friends and colleagues who may find it useful. Have a great day.

  • @philtoa334
    @philtoa334 3 роки тому

    Really good thx.