\somefunction
I have remove \textit
entirely. In math mode variables (a-zA-Zα-ο) are usually typeset in italic shape. Math mode takes care of the italic correction (between F
and (
in this case).
If you want to use \somefunction
in text, use always inline math (\(…\)
).
It wouldn't be that wrong to use a simple definition for the entity (pd
) and “fix” the spacing inside \somefunction
(or better: entityFunction
/\funcOfEnt
/…) except when parenthesized entities will be used more often and outside of a function, like (\entity{pd} + \entity{qr}) x
, than the definition of the function should use the already patched \entity
to be more consistent.
\textit
and \texttt
I don't think there is a total automatic solution available for fixing the problem with )
and following punctuation.
Let's take a look at the options:
\ittt
Your original \ittt
was defined as
\newcommand*{\ittt}[1]{\textit{\texttt{#1}}}
\itttm
If you only use your entities in text mode before punctuation, \itttm
can work for you. It adds italic correction only in math mode.
\newcommand*{\itttm}[1]{%
\ifmmode\mmodebeforetrue\else\mmodebeforefalse\fi%
\textit{\texttt{#1\ifmmodebefore\/\fi}}%
}
\itttp
The macro \itttp
typsets a second optional parameter in italic shape (but not in the typewriter family). This has been discussed on TeX.SE already.
In my eyes, this is even too tight.
\DeclareDocumentCommand{\itttp}{m O{}}{%
\textit{\texttt{#1}#2\/}%
}
\ttit
Switching \textit
and \texttt
gives us italic correction. But it also adds this italic correction before punctuation mark where you don't want it (i.e., .
and ,
).
\newcommand*{\ttit}[1]{\texttt{\textit{#1}}}
\ttitc
Like \ttit
, but:
The if-macro \@ifnextchar
removes the italic correction before .
and ,
with a negative kern.
I also tested both variants with \emph
:
\emph{\texttt{pd}}
is a little better before )
, and as good as \ittt
before punctuation
\texttt{\emph{pd}}
is as bad as \ttit
.
\entity[*]{<arg>}
I also declared another macro: \entity
. It does exist in a non-starred and a starred form.
Use the starred form if you do not want italic correction (i.e. before .
and ,
).
Conclusion
I think the best solutions are:
\itttm
if you need italic correction only in math mode.
\ttitc
if can handle a few cases where the automatically inserted correction has to be re-corrected
\entity
if you're willing to use a switch (the *
) to toggle italic correction (it should be doable to use \@ifnextchar
in \entity
, too).
Code
\documentclass{article}
\usepackage{times}
\usepackage{txfonts}
\usepackage{array,multirow,xcolor} % for presentation
\def\dont{\color[gray]{.5}} % *
\usepackage{xparse}% for \itttp and \entity
\newif\ifmmodebefore% for \itttm
\newcommand*{\somefunction}[1]{F(#1)}
\newcommand*{\ittt}[1]{\textit{\texttt{#1}}}
\newcommand*{\ttit}[1]{\texttt{\textit{#1}}}
\newcommand*{\itttm}[1]{% \itttc inserts italic correction \/ if in math mode
\ifmmode\mmodebeforetrue\else\mmodebeforefalse\fi%
\textit{\texttt{#1\ifmmodebefore\/\fi}}%
}
\makeatletter % needed because @ is used in macro name
\newcommand*{\ttitc}[1]{% \ttitc checks if . or , follows and kerns
\texttt{\textit{#1}}%
\@ifnextchar.{\kern-.1em}{\@ifnextchar,{\kern-.08em}{}}%
}
\makeatother
\DeclareDocumentCommand{\itttp}{m O{}}{% optional punctuation will be typeset italic
\textit{\texttt{#1}#2\/}%
}
\DeclareDocumentCommand{\entity}{s m}{% star = no italic correction (before , and .)
\textit{\texttt{#2}%
\IfBooleanTF{#1}%
{}%
{\/}%
}%
}
%\usepackage{lua-visual-debug} % use this with LuaLaTeX for debug-mode
\begin{document}\noindent
\begin{tabular}{l@{}c@{ }c@{ }c}
macro & math & \verb|\times| & text \\ \hline& text mode \\ \hline
\verb|\ittt{pd}| & $(\ittt{pd} )$ & $ \ittt{pd} \times {}$ & \ittt{pd}, \ittt{pd}. \\ \hline
\verb|\itttm{pd}| & $(\itttm{pd})$ & $ \itttm{pd} \times {}$ & \itttm{pd}, \itttm{pd}. \\
\verb|\itttp{pd}| & $(\itttp{pd})$ & $ \itttp{pd} \times {}$ & \dont \itttp{pd}, \itttp{pd}. \\
\verb|\itttp{pd}[.]| & --- & --- & \itttp{pd}[,] \itttp{pd}[.] \\ \hline
\verb|\ttit{pd}| & \multirow{2}{*}{$(\ttit{pd} )$} & \multirow{2}{*}{$ \ttit{pd} \times {}$} & \ttit{pd}, \ttit{pd}. \\
\verb|\ttitc{pd}| & & & \ttitc{pd}, \ttitc{pd}. \\ \hline
\verb|\entity{pd}| & $(\entity{pd})$ & $ \entity{pd} \times {}$ & \dont \entity{pd}, \entity{pd}. \\
\verb|\entity*{pd}| & --- & --- & \entity*{pd}, \entity*{pd}. \\
\end{tabular}
\end{document}
Output
![Output table Output table](https://i.stack.imgur.com/Io7J4.png)
Here's a LuaLaTeX-based solution. It sets up a Lua function called nabla_f
that does most of the work, and it assigns this function to the process_input_buffer
callback, making it operate on all inputs before TeX starts is usual processing.
![enter image description here](https://i.stack.imgur.com/YkcvK.png)
% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{times,mtpro2} % text and math fonts
\usepackage{luacode}
\begin{luacode}
function nabla_f ( s )
return ( s:gsub ( "\\nabla%s*f" , "\\nabla\\mkern-4mu f" ) )
end
\end{luacode}
\directlua{luatexbase.add_to_callback("process_input_buffer", nabla_f , "nablaf" )}
\begin{document}
$\nabla{}f$ $\nabla f$ $\nabla h$ $\nabla k$
\end{document}
Addendum: If you also want to change the amount of whitespace between \partial
and f
, while keeping the instruction to change the distance between \nabla
and f
, I suggest you replace
return ( s:gsub ( "\\nabla%s*f" , "\\nabla\\mkern-4mu f" ) )
with
s = s:gsub ( "\\nabla%s*f" , "\\nabla\\mkern-4mu f" )
s = s:gsub ( "\\partial%s*f", "\\partial\\mkern-4mu f" )
return s
Of course, you can (and should) change the new instance of -4mu
to whatever the optimal adjustment amount may be.
Best Answer
By default, LaTeX gobbles all spaces after any command. So, you can have three solutions here:
\
after\textquoteright
followed by a space.'
instead of\textquoteright
.\xspace
command fromxspace
package to get intelligent spaces.See the solutions below: