  The modification of the production of $J/\psi$, $\psi(\mathrm{2S})$, and
$\mit{\Upsilon}(n\mathrm{S})$ ($n = 1, 2, 3$) in $p$+Pb collisions with respect
to their production in $pp$ collisions has been studied. The $p$+Pb and $pp$
datasets used in this paper correspond to integrated luminosities of $28$
$\mathrm{nb}^{-1}$ and $25$ $\mathrm{pb}^{-1}$ respectively, collected in 2013
and 2015 by the ATLAS detector at the LHC, both at a centre-of-mass energy per
nucleon pair of 5.02 TeV. The quarkonium states are reconstructed in the dimuon
decay channel. The yields of $J/\psi$ and $\psi(\mathrm{2S})$ are separated
into prompt and non-prompt sources. The measured quarkonium differential cross
sections are presented as a function of rapidity and transverse momentum, as is
the nuclear modification factor, $R_{p\mathrm{Pb}}$ for $J/\psi$ and
$\mit{\Upsilon}(\mathrm{1S})$. No significant modification of the $J/\psi$
production is observed while $\mit{\Upsilon}(\mathrm{1S})$ production is found
to be suppressed at low transverse momentum in $p$+Pb collisions relative to
$pp$ collisions. The production of excited charmonium and bottomonium states is
found to be suppressed relative to that of the ground states in central $p$+Pb
collisions.
