2

I am writing a document that has a lot of images which are basically extracts from other PDF's.

What I do now is to copy the image, paste it in MSPaint and then I save it in the path folder that I'm using for \includegraphics{path}.

Is there any way one can simply this extra work by copying the image and paste it directly into the editor from the clipboard? I'm sure there is no straight-forward way, but can there be any trick that would convert the path or the image to a \includegraphics command?

I have noticed that if one pastes an image which is in clipboard to the tex editor, nothing happens. Is there any way to at least convert that image to the file path?

I have drawn what I have in mind, but please correct me if I'm dreaming of unicorns: ![Macro Sketch

Or does anyone know any faster way to do that? It sux when you have a lot of pictures.

EDIT: After reading the answers from Hamid I have realized that it is about setting up a Macro that:

  1. Saves the image from the clipboard to a file
  2. Reads the name and includes in a preset code (\begin{figure} \includegraphics{path_created_by_macro} \end{figure})
  3. Writes this in the editor.

3 Answers 3

6

If you are working in Linux you can use a bash script like this:

#!/bin/bash
for f in `ls  ./myImageDIR/*.png;`
do
  echo '
  \begin{figure}
        \includegraphics{'$f'}
  \end{figure}' 
done

and, following the tips in: How to execute shell script from LaTeX?, compile the file test.tex:

\documentclass{article}
\usepackage{graphicx}
\immediate\write18{./my-shell-script.sh > scriptoutput.tex}
\begin{document}
\input{scriptoutput.tex}
\end{document}

with the command line:

pdflatex -shell-escape -enable-write18 test.tex

If you need to add some text:

\documentclass{article}
\usepackage{graphicx}
\begin{document}
text ...
\immediate\write18{./InsertOneImageLatex.sh  firstPicture}
\input{firstPicture.tex}
...other text...
\immediate\write18{./InsertOneImageLatex.sh  SecondPicture}
\input{SecondPicture.tex}
\end{document}

with InsertOneImageLatex.sh:

#!/bin/bash
echo '
\begin{figure}
        \includegraphics{'./myImageDIR/$1.png'}
\end{figure}' > $1.tex; 
11
  • Okay. I just tried it. I get ./Script.txt: line 3: syntax error near unexpected token $'do\r'' '/Script.txt: line 3: do
    – Physther
    Commented Oct 21, 2016 at 11:24
  • try to remove \r with: tr -d "\r" < Script.txt stackoverflow.com/questions/3134791/… Commented Oct 21, 2016 at 11:29
  • Hmm. Weird. I tried both with "tr -d '\n' < yourfile.txt" and "tr -d "\n\r" < yourfile.txt", as recommended by Tyler... I get this after writing the code: #!/bin/bashfor f in 'ls ./myImageDIR/*.png;'do echo ' \begin{figure} \includegraphics{'$f'} \end{figure}' >> new.texdone And after writing ./Script, I get the same error: ./Script.txt: line 3: syntax error near unexpected token $'do\r'' '/Script.txt: line 3: do
    – Physther
    Commented Oct 21, 2016 at 11:40
  • Probably it depends on how you save your script ... you should use Unix/Linux for 'Line ending' Commented Oct 21, 2016 at 11:46
  • 1
    When you save your file choose 'Save as' and change the file format o the encoding. Commented Oct 21, 2016 at 12:13
4

You can use TeXstudio. Simply drag and drop the image into it. Also for the first time you may need to pre-set some parameters in the wizard.

If the number of images is very high. You may also write some script to import all the images at once using javascript in TeXstudio.

11
  • You mean "TexStudio"?
    – Physther
    Commented Oct 21, 2016 at 10:55
  • Okay. I looked for it. It's an editor, but this also requires that I extract the picture, put it in MSpaint and then take the path and copy-paste the image in the editor. I just did that and it works also with TexMaker (thanks for the advice thought! I didn't know about this)
    – Physther
    Commented Oct 21, 2016 at 10:57
  • 2
    in texstudio you may write a javascript macro which would paste from clipboard. Then assign a shortcut to it for the ease of use. but writing macro needs a little work too:).
    – Hamid
    Commented Oct 21, 2016 at 11:01
  • 1
    The macro section in texmaker is not as handy as texstudio and I didn't work with it. Also, it uses another name instead of macro. Totally, I'm advising texstudio for this kind of batch processing
    – Hamid
    Commented Oct 21, 2016 at 11:05
  • 1
    In Windows, you can simply grab part of your screen with Windows+S, then paste in TexStudio. This is an absolute game changer when doing bibliography work for example.
    – P-Gn
    Commented Dec 11, 2020 at 17:44
1

Extending the answer by Arianna Angeletti, if the images are in separate subfolders inside a parent folder, then the following bash script will create a text file with latex code for all images in each subfolder. This script must be executed inside the parent folder (the parent folder must not contain pictures itself). The latex for inserting images can be formatted as needed. The file names must not include spaces.

#!/bin/bash
#Find all directories in a parent folder
read -p "Enter image file extension (jpg, png, pdf, ...) " ext
DIRS=()
for i in * ; do 
  if [[ -d "$i" ]] && DIRS+=("$i") ; then
    echo "$i" 
  fi
done
# Create a latex code for each image in the subfolder 
#and write the latex code in a file figureFileNames.txt
for dir in "${DIRS[@]}" ; do
   arr=$(cd $dir && ls *.$ext) 
   for f in $arr;
    do
        postfix=".${ext}"
        f=$f
        f=${f%"$postfix"}
        echo '
        \begin{figure}
        \includegraphics{'$f'}
        \end{figure}' >> $dir/figureFileNames.txt
    done 
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .