具有三个输入参数的 Latex newcommand

具有三个输入参数的 Latex newcommand

我只是出于好奇才问这个问题。

\documentclass{article}
\usepackage[english]{babel}
\usepackage[left=2.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm, noheadfoot]{geometry}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{setspace}
\usepackage{etoolbox}
\setlength\parindent{0pt}
\doublespacing

\BeforeBeginEnvironment{wrapfigure}{\setlength{\intextsep}{0pt}}
\newcommand*\longName[1]{\par{\raggedright\textbf{#1}\par}}

我使用下面的命令几次,大约 40-50 次。有没有办法定义一个带有 3 个不同输入参数的命令。每次Long name text,、figure location和都会Lorem Ipsum text改变。我的输入将是这样的:\myThreeParam{long name text}{figure location}{Lorem Ipsum text}或者还有其他方法。

\longName{Long name text}

\begin{wrapfigure}{l}{0pt}
    \includegraphics[scale=0.8]{example-image-a}
\end{wrapfigure}

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\newline

整个文件

\documentclass{article}
\usepackage[english]{babel}
\usepackage[left=2.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm, noheadfoot]{geometry}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{setspace}
\usepackage{etoolbox}
\setlength\parindent{0pt}
\doublespacing

\BeforeBeginEnvironment{wrapfigure}{\setlength{\intextsep}{0pt}}
\newcommand*\longName[1]{\par{\raggedright\textbf{#1}\par}}

\begin{document}

\longName{Long name 1}

\begin{wrapfigure}{l}{0pt}
    \includegraphics[scale=0.8]{example-image}
\end{wrapfigure}

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\newline


\longName{Long name 2}

\begin{wrapfigure}{l}{0pt}
    \includegraphics[scale=0.8]{example-image-b}
\end{wrapfigure}

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.\newline

\end{document}

答案1

这需要一个环境而不是一个三参数命令。

\documentclass{article}
\usepackage[english]{babel}
\usepackage[left=2.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm, noheadfoot]{geometry}
\usepackage{graphicx}
\usepackage{wrapfig}

\NewDocumentEnvironment{mywrap}{mO{scale=0.8}m}
 {\par{\raggedright\bfseries #1\par}\nopagebreak
  \begin{wrapfigure}{l}{0pt}
  \includegraphics[#2]{#3}
  \end{wrapfigure}
  \noindent\ignorespaces
 }{\par}

\setlength{\intextsep}{0pt}

\begin{document}

\begin{mywrap}{Long name 1}[scale=0.8]{example-image}
Lorem Ipsum is simply dummy text of the printing and typesetting 
industry. Lorem Ipsum has been the industry's standard dummy text 
ever since the 1500s, when an unknown printer took a galley of 
type and scrambled it to make a type specimen book. It has survived 
not only five centuries, but also the leap into electronic typesetting, 
remaining essentially unchanged. It was popularised in the 1960s with 
the release of Letraset sheets containing Lorem Ipsum passages, and 
more recently with desktop publishing software like Aldus PageMaker 
including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text 
of the printing and typesetting industry. Lorem Ipsum has been the 
industry's standard dummy text ever since the 1500s, when an unknown 
printer took a galley of type and scrambled it to make a type specimen 
book. It has survived not only five centuries, but also the leap into 
electronic typesetting, remaining essentially unchanged. It was 
popularised in the 1960s with the release of Letraset sheets containing 
Lorem Ipsum passages, and more recently with desktop publishing software 
like Aldus PageMaker including versions of Lorem Ipsum.
\end{mywrap}

\begin{mywrap}{Long name 1}[width=3cm]{example-image}
Lorem Ipsum is simply dummy text of the printing and typesetting 
industry. Lorem Ipsum has been the industry's standard dummy text 
ever since the 1500s, when an unknown printer took a galley of 
type and scrambled it to make a type specimen book. It has survived 
not only five centuries, but also the leap into electronic typesetting, 
remaining essentially unchanged. It was popularised in the 1960s with 
the release of Letraset sheets containing Lorem Ipsum passages, and 
more recently with desktop publishing software like Aldus PageMaker 
including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text 
of the printing and typesetting industry. Lorem Ipsum has been the 
industry's standard dummy text ever since the 1500s, when an unknown 
printer took a galley of type and scrambled it to make a type specimen 
book. It has survived not only five centuries, but also the leap into 
electronic typesetting, remaining essentially unchanged. It was 
popularised in the 1960s with the release of Letraset sheets containing 
Lorem Ipsum passages, and more recently with desktop publishing software 
like Aldus PageMaker including versions of Lorem Ipsum.
\end{mywrap}

\end{document}

我添加了一个可选参数来将选项传递给\includegraphics,以便您可以在本地覆盖默认的scale=0.8。这样\nopagebreak我们就可以避免长文本在一个页面中,而段落在下一个页面中。

在此处输入图片描述

答案2

正如您已经展示的那样,您的问题并不十分清楚\newcommand,这只是另一个例子。无关紧要,永远不要用\newline

\documentclass{article}
\usepackage[english]{babel}
\usepackage[left=2.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm, noheadfoot]{geometry}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{setspace}
\usepackage{etoolbox}
\setlength\parindent{0pt}
\doublespacing

\BeforeBeginEnvironment{wrapfigure}{\setlength{\intextsep}{0pt}}
\newcommand*\longName[1]{\par{\raggedright\textbf{#1}\par}}
\newcommand\myThreeParam[3]{%
\longName{#1}%
\begin{wrapfigure}{l}{0pt}%
    \includegraphics[scale=0.8]{#2}%
\end{wrapfigure}%
#3\par}

\begin{document}

\myThreeParam{Long name 1}{example-image}{%
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.} %NO!!!!\newline


\myThreeParam{Long name 2}{example-image-b}{%
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.} %NO!!!!\newline

\end{document}

相关内容