我一直在为一些分段函数编写一组简单的线条绘制宏。出于某种原因,当我尝试为线条添加颜色时,每个段都会向右平移。
我已验证这不是由于共享变量名引起的,并在下面放置了一个 MWE 以及输出。从 \drawthedamnline 宏中删除 \color 语句会产生正确的输出(线段是连续的)。我怀疑这与我编写宏的方式有关。任何建议都会有所帮助。
\documentclass[12pt]{article}
\usepackage[nomessages]{fp}
\usepackage{color}
\usepackage{wrapfig}
\usepackage{amsmath}
\usepackage{ifthen}
\newcommand{\ticks}[5]{ %x,y,numticks,direction (x=0,y=1),tickwidth
\ifthenelse{#3=0}{
}
{
\ifthenelse{#4=0}{
\FPadd{\newx}{#1}{#5}
\FPset{\newy}{#2}
\put(\newx,\newy){\line(0,1){5}}
}
{
\FPset{\newx}{#1}
\FPadd{\newy}{#2}{#5}
\put(\newx,\newy){\line(1,0){5}}
}
\FPsub{\numticksleft}{#3}{1}
\FPclip{\numticksleft}{\numticksleft}
\ticks{\newx}{\newy}{\numticksleft}{#4}{#5}
}
}
\newcommand{\peakplot}[5]{ %x,y,height,width,num_candidates
\put(#1,#2){\vector(0,1){#3}}
\put(#1,#2){\vector(1,0){#4}}
\FPsub{\tickspacex}{#3}{10}
\FPsub{\tickspacey}{#4}{10}
\FPdiv{\tickwidth}{\tickspacex}{#5}
\FPclip{\tickwidth}{\tickwidth}
\ticks{#1}{#2}{#5}{1}{\tickwidth}
\FPdiv{\tickwidth}{\tickspacey}{#5}
\ticks{#1}{#2}{#5}{0}{\tickwidth}
}
\newcommand{\drawthedamnline}[1]{
\color{\mylinecolor}
\put(\mypeakx, \peaky){\line(1,#1){\peakspace}}
}
\newcommand{\plotpeaks}[1]{ %used by \peak to plot a peak.
\drawthedamnline{#1}
\FPadd{\mypeakx}{\mypeakx}{\peakspace}
\FPmul{\ymove}{\peakspace}{#1}
\FPadd{\peaky}{\ymove}{\peaky}
\ifthenelse{\numpeaks=1}{}{
\FPsub{\numpeaks}{\numpeaks}{1}
\FPclip{\numpeaks}{\numpeaks}
\plotpeaks
}
}
\newcommand{\peak}[5]{ %x,y, num_args, width, color, arg1, arg2,...argn.
\FPset{\mypeakx}{#1}
\FPset{\peaky}{#2}
\FPset{\numpeaks}{#3}
\FPdiv{\peakspace}{#4}{#3}
\FPclip{\peakspace}{\peakspace}
\def \mylinecolor {#5}
\plotpeaks
}
\begin{document}
\begin{wrapfigure}{r}{0.33\textwidth}
\begin{center}
\begin{picture}(100,100)
\thicklines
\peakplot{0}{0}{100}{100}{5}
\peak{0}{0}{5}{90}{red}{1}{1}{1}{1}{1}
\peak{0}{90}{5}{90}{blue}{-1}{-1}{-1}{-1}{-1}
\peak{0}{0}{5}{90}{green}{2}{1}{2}{-1}{-1}
\end{picture}
\end{center}
\caption{}
\label{condorcetFig}
\end{wrapfigure}
\end{document}
答案1
您有大量未受保护的行尾,可能会在输出中产生空格。
\documentclass[12pt]{article}
\usepackage[nomessages]{fp}
\usepackage{color}
\usepackage{wrapfig}
\usepackage{amsmath}
\usepackage{ifthen}
\newcommand{\ticks}[5]{%x,y,numticks,direction (x=0,y=1),tickwidth
\ifthenelse{#3=0}
{}%
{%
\ifthenelse{#4=0}
{%
\FPadd{\newx}{#1}{#5}%
\FPset{\newy}{#2}%
\put(\newx,\newy){\line(0,1){5}}%
}%
{%
\FPset{\newx}{#1}%
\FPadd{\newy}{#2}{#5}%
\put(\newx,\newy){\line(1,0){5}}%
}%
\FPsub{\numticksleft}{#3}{1}%
\FPclip{\numticksleft}{\numticksleft}%
\ticks{\newx}{\newy}{\numticksleft}{#4}{#5}%
}%
}
\newcommand{\peakplot}[5]{%x,y,height,width,num_candidates
\put(#1,#2){\vector(0,1){#3}}%
\put(#1,#2){\vector(1,0){#4}}%
\FPsub{\tickspacex}{#3}{10}%
\FPsub{\tickspacey}{#4}{10}%
\FPdiv{\tickwidth}{\tickspacex}{#5}%
\FPclip{\tickwidth}{\tickwidth}%
\ticks{#1}{#2}{#5}{1}{\tickwidth}%
\FPdiv{\tickwidth}{\tickspacey}{#5}%
\ticks{#1}{#2}{#5}{0}{\tickwidth}%
}
\newcommand{\drawthedamnline}[1]{%
\color{\mylinecolor}%
\put(\mypeakx, \peaky){\line(1,#1){\peakspace}}%
}
\newcommand{\plotpeaks}[1]{%used by \peak to plot a peak.
\drawthedamnline{#1}%
\FPadd{\mypeakx}{\mypeakx}{\peakspace}%
\FPmul{\ymove}{\peakspace}{#1}%
\FPadd{\peaky}{\ymove}{\peaky}%
\ifthenelse{\numpeaks=1}
{}
{%
\FPsub{\numpeaks}{\numpeaks}{1}%
\FPclip{\numpeaks}{\numpeaks}%
\plotpeaks
}%
}
\newcommand{\peak}[5]{%x,y, num_args, width, color, arg1, arg2,...argn.
\FPset{\mypeakx}{#1}%
\FPset{\peaky}{#2}%
\FPset{\numpeaks}{#3}%
\FPdiv{\peakspace}{#4}{#3}%
\FPclip{\peakspace}{\peakspace}%
\def\mylinecolor{#5}%
\plotpeaks
}
\begin{document}
\begin{wrapfigure}{r}{0.33\textwidth}
\begin{center}
\begin{picture}(100,100)
\thicklines
\peakplot{0}{0}{100}{100}{5}%
\peak{0}{0}{5}{90}{red}{1}{1}{1}{1}{1}%
\peak{0}{90}{5}{90}{blue}{-1}{-1}{-1}{-1}{-1}%
\peak{0}{0}{5}{90}{green}{2}{1}{2}{-1}{-1}%
\end{picture}
\end{center}
\caption{}
\label{condorcetFig}
\end{wrapfigure}
\end{document}
答案2
放进\color{mylinecolor}
去:
\newcommand{\drawthedamnline}[1]{% %% <---Spurious space
%\color{\mylinecolor}
\put(\mypeakx, \peaky){\color{\mylinecolor}\line(1,#1){\peakspace}}
}
完整代码:
\documentclass[12pt]{article}
\usepackage[nomessages]{fp}
\usepackage{color}
\usepackage{wrapfig}
\usepackage{amsmath}
\usepackage{ifthen}
\newcommand{\ticks}[5]{%x,y,numticks,direction (x=0,y=1),tickwidth
\ifthenelse{#3=0}{
}
{
\ifthenelse{#4=0}{
\FPadd{\newx}{#1}{#5}
\FPset{\newy}{#2}
\put(\newx,\newy){\line(0,1){5}}
}
{
\FPset{\newx}{#1}
\FPadd{\newy}{#2}{#5}
\put(\newx,\newy){\line(1,0){5}}
}
\FPsub{\numticksleft}{#3}{1}
\FPclip{\numticksleft}{\numticksleft}
\ticks{\newx}{\newy}{\numticksleft}{#4}{#5}
}
}
\newcommand{\peakplot}[5]{%x,y,height,width,num_candidates
\put(#1,#2){\vector(0,1){#3}}
\put(#1,#2){\vector(1,0){#4}}
\FPsub{\tickspacex}{#3}{10}
\FPsub{\tickspacey}{#4}{10}
\FPdiv{\tickwidth}{\tickspacex}{#5}
\FPclip{\tickwidth}{\tickwidth}
\ticks{#1}{#2}{#5}{1}{\tickwidth}
\FPdiv{\tickwidth}{\tickspacey}{#5}
\ticks{#1}{#2}{#5}{0}{\tickwidth}
}
\newcommand{\drawthedamnline}[1]{%
%\color{\mylinecolor}
\put(\mypeakx, \peaky){\color{\mylinecolor}\line(1,#1){\peakspace}}
}
\newcommand{\plotpeaks}[1]{%used by \peak to plot a peak.
\drawthedamnline{#1}
\FPadd{\mypeakx}{\mypeakx}{\peakspace}
\FPmul{\ymove}{\peakspace}{#1}
\FPadd{\peaky}{\ymove}{\peaky}
\ifthenelse{\numpeaks=1}{}{
\FPsub{\numpeaks}{\numpeaks}{1}
\FPclip{\numpeaks}{\numpeaks}
\plotpeaks
}
}
\newcommand{\peak}[5]{ %x,y, num_args, width, color, arg1, arg2,...argn.
\FPset{\mypeakx}{#1}
\FPset{\peaky}{#2}
\FPset{\numpeaks}{#3}
\FPdiv{\peakspace}{#4}{#3}
\FPclip{\peakspace}{\peakspace}
\def \mylinecolor {#5}
\plotpeaks
}
\begin{document}
\begin{wrapfigure}{r}{0.33\textwidth}
\begin{center}
\begin{picture}(100,100)
\thicklines
\peakplot{0}{0}{100}{100}{5}
\peak{0}{0}{5}{90}{red}{1}{1}{1}{1}{1}
\peak{0}{90}{5}{90}{blue}{-1}{-1}{-1}{-1}{-1}
\peak{0}{0}{5}{90}{green}{2}{1}{2}{-1}{-1}
\end{picture}
\end{center}
\caption{}
\label{condorcetFig}
\end{wrapfigure}
\end{document}