# Macro for Copy/Paste Image in Tex file

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:

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.

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;  • Okay. I just tried it. I get ./Script.txt: line 3: syntax error near unexpected token $'do\r'' '/Script.txt: line 3: do – Physther Oct 21 '16 at 11:24
• try to remove \r with: tr -d "\r" < Script.txt stackoverflow.com/questions/3134791/… – Arianna Angeletti Oct 21 '16 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 Oct 21 '16 at 11:40
• Probably it depends on how you save your script ... you should use Unix/Linux for 'Line ending' – Arianna Angeletti Oct 21 '16 at 11:46
• When you save your file choose 'Save as' and change the file format o the encoding. – Arianna Angeletti Oct 21 '16 at 12:13

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.

• You mean "TexStudio"? – Physther Oct 21 '16 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 Oct 21 '16 at 10:57
• I would like to take it from the clipboard somehow. – Physther Oct 21 '16 at 10:58
• 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 Oct 21 '16 at 11:01
• Okay. So it is possible. Can one do the same in TeXMaker? – Physther Oct 21 '16 at 11:03