\documentclass[10pt]{article}
\usepackage[default]{opensans}
\usepackage[T1]{fontenc}
\usepackage{xifthen}
\newcommand{\cvAnrede}{Herr}
\newcommand{\cvTitel}{Dr.}
\newcommand{\cvVorname}{Max}
\newcommand{\cvNachname}{Mustermann}
\newcommand{\cvSetTitel}[1]
{
\ifthenelse{\equal{#1}{}}{}{\cvTitel}
}
\newcommand{\setName}[1]
{
\ifthenelse{\equal{#1}{nachname}}{\cvAnrede{} \cvSetTitel{\cvTitel} \cvNachname }{\cvVorname }
}
\newcommand{\setCompleteName}[1]
{
\ifthenelse{\equal{#1}{nachname}}{\cvSetTitel{\cvTitel} \cvVorname \cvNachname }{\cvVorname}
}
\newcommand{\cvName}{\setName{\cvSetName}}
\newcommand{\cvFullName}{\setCompleteName{\cvSetName}}
\begin{document}
\ifdefined\cvSetName
\else
\def\cvSetName{nachname}
\fi
\cvName \\
Herr Dr. Mustermann
\end{document}
输出:
我的错误是什么?我不明白为什么 1. 有间距和 2. 有不同的间距。
答案1
你的定义中有虚假空格——行尾算数。用 % 隐藏它们
\documentclass[10pt]{article}
\usepackage[default]{opensans}
\usepackage[T1]{fontenc}
\usepackage{xifthen}
\newcommand{\cvAnrede}{Herr}
\newcommand{\cvTitel}{Dr.}
\newcommand{\cvVorname}{Max}
\newcommand{\cvNachname}{Mustermann}
\newcommand{\cvSetTitel}[1]
{%
\ifthenelse{\equal{#1}{}}{}{\cvTitel}%
}
\newcommand{\setName}[1]
{%
\ifthenelse{\equal{#1}{nachname}}{\cvAnrede{} \cvSetTitel{\cvTitel} \cvNachname }{\cvVorname }%
}
\newcommand{\setCompleteName}[1]
{%
\ifthenelse{\equal{#1}{nachname}}{\cvSetTitel{\cvTitel} \cvVorname \cvNachname }{\cvVorname}%
}
\newcommand{\cvName}{\setName{\cvSetName}}
\newcommand{\cvFullName}{\setCompleteName{\cvSetName}}
\begin{document}
\ifdefined\cvSetName
\else
\def\cvSetName{nachname}
\fi
\cvName \\
Herr Dr. Mustermann
\end{document}