Derive utility function with both substitutes and complements I know that in the 2-good world it is easy to derive the demand functions from a utility function for imperfect substitutes or complements, but what if I have N goods that include many combinations of substitutes and complements?

For example if you have hot dogs $D$, hamburgers $H$ (substitutes of each other), and mustard $M$, mayo $Y$ (substitutes of each other, complements with hot dogs and hamburgers), I can try something like this:

$$
U(D, H, M, Y) = (D^{0.5} + H^{0.5})(M^{0.5} + Y^{0.5})
$$

Unfortunately the math gets pretty hairy for this one pretty quickly. Before I dive in much further with solving these equations, is there a well-known function that is used in the literature for this type of analysis?

Note: I am interested in the more general N-good case, not just the 4-good one.
The general way to do this would be using a nested CES Function.  CES Wikipedia

For your example you could define the utility of sandwiches (S) and condiments (C) to be 

$$ U(D,H,M,Y) = (a_1 S^{\frac{s-1}{s}} + a_2 C^{\frac{s-1}{s}})^{\frac{s}{s-1}} $$
then you can define $S$ and $C$ "nests" as
$$ S = (b_1 D^{\frac{\rho-1}{\rho}} + b_2 H^{\frac{\rho-1}{\rho}})^{\frac{\rho}{\rho-1}} $$
$$ C = (c_1 M^{\frac{\eta-1}{\eta}} + c_2 Y^{\frac{\eta-1}{\eta}})^{\frac{\eta}{\eta-1}} $$

$s$ determines whether S and C are complements ($s \rightarrow 0$) or substitutes ($s \rightarrow \infty$), or neither ($s=1$).

The same holds for $\rho$ and $\eta$ in the "nests".  $a$, $b$, and $c$ establish the relative importance of each item within a nest. 

Extending this to $N$ you can have an arbitrary number of nests and an arbitrary number of elements within each nest.  This way may look complicated, but it has a lot of flexibility and gives you nice derivatives when solving maximization problems.