1

I would use the following code many times in my latex note.

\begin{wrapfigure}[5]{r}{10ex}
\vspace{-2ex}
\includegraphics[width=10ex]{any_picture.jpg}
\end{wrapfigure}

However it will be clumsy if I change one parameter(for example, change {10ex} to {5ex}) for all the pictures. How to custom wrapfigure environment in preamble so that I can easily change the parameters of all pictures? The following is my code, please help me, thank you.

\documentclass[a4paper,12pt]{article}
\usepackage{titletoc} 
\usepackage{graphicx, subfigure} 
\usepackage{wrapfig} 

\begin{document}
\begin{wrapfigure}[5]{r}{10ex}
\vspace{-2ex}
\includegraphics[width=10ex]{my_picture.jpg}
\end{wrapfigure}
text text text text text text text text text text text text text text text text text text text text text 

\begin{wrapfigure}[5]{r}{10ex}
\vspace{-2ex}
\includegraphics[width=10ex]{my_picture.jpg}
\end{wrapfigure}
text text text text text text text text text text text text text text text text text text text text text 

\begin{wrapfigure}[5]{r}{10ex}
\vspace{-2ex}
\includegraphics[width=10ex]{my_picture.jpg}
\end{wrapfigure}
text text text text text text text text text text text text text text text text text text text text text 
\end{document} 
3

That is why you have macros.

\newlength{\mywidth}
\setlength{\mywidth}{10ex}
\newcommand{\mypos}{r}
\newcommand{\mylines}{5}

Change parameters in the last three lines and in your code use them like this:

\documentclass[a4paper,12pt]{article}
\usepackage{titletoc}
\usepackage{graphicx, subfigure}
\usepackage{wrapfig}
\newlength{\mywidth}
\setlength{\mywidth}{10ex}   %% <---- change here
\newcommand{\mypos}{r}       %% <---- change here
\newcommand{\mylines}{5}     %% <---- change here

\begin{document}
\begin{wrapfigure}[\mylines]{\mypos}{\mywidth}
\vspace{-2ex}
\includegraphics[width=\mywidth]{example-image-a}
\end{wrapfigure}
text text text text text text text text text text text text text text text text text text text text text

\begin{wrapfigure}[\mylines]{\mypos}{\mywidth}
\vspace{-2ex}
\includegraphics[width=\mywidth]{example-image-a}
\end{wrapfigure}
text text text text text text text text text text text text text text text text text text text text text

\begin{wrapfigure}[\mylines]{\mypos}{\mywidth}
\vspace{-2ex}
\includegraphics[width=\mywidth]{example-image-a}
\end{wrapfigure}
text text text text text text text text text text text text text text text text text text text text text
\end{document}
  • There is a link for defining the new function tex.stackexchange.com/questions/129345/… But it's fault when compiling my picture. However how to write the function(or the format of \newcommand) like in the link? Please supply the link of material about \newcommand. I want to learn more about \newcommand and macros. Thank you very much – Hyden Mar 9 '15 at 1:42
  • @Hyden Instead of saying its faulty, can you please add that cpde to the question?. BTW you have to have more text in your paragraphs. – user11232 Mar 9 '15 at 6:01
0

Inspired by the code that Harish Kumar posted. There is my another suitable answer following.

\documentclass[a4paper,12pt]{article}
\usepackage{titletoc}
\usepackage{graphicx, subfigure}
\usepackage{wrapfig}

\newcommand{\testwrap}[1]
{\begin{wrapfigure}[5]{r}{10ex}
\vspace{-2ex}
\includegraphics[width=10ex]{#1}
\end{wrapfigure}}


\begin{document}
\testwrap{example.jpg}
text text text text text text text text text text text text text text text text text text text text text

\testwrap{example.jpg}
text text text text text text text text text text text text text text text text text text text text text

\testwrap{example.jpg}
text text text text text text text text text text text text text text text text text text text text text
\end{document} 

I just define the \testwrap as the set of (macro)instruction. That's the macros that Harish Kumar mentioned. Thanks Harish Kumar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.