We present a semianalytical theory for the acoustic fields and particle-trapping forces in a viscous fluid inside a capillary tube with arbitrary cross section and ultrasound actuation at the walls. We find that the acoustic fields vary axially on a length scale proportional to the square root of the quality factor of the two-dimensional (2D) cross-section resonance mode. This axial variation is determined analytically based on the numerical solution to the eigenvalue problem in the 2D cross section. The analysis is developed in two steps: First, we generalize a recently published expression for the 2D standing-wave resonance modes in a rectangular cross section to arbitrary shapes, including the viscous boundary layer. Second, based on these 2D modes, we derive analytical expressions in three dimensions for the acoustic pressure, the acoustic radiation and trapping force, as well as the acoustic energy flux density. We validate the theory by comparison to three-dimensional numerical simulations.