We generalize and unify the $$f\left( R,T\right) $$
f
R
,
T
and $$f\left( R,L_m\right) $$
f
R
,
L
m
type gravity models by assuming that the gravitational Lagrangian is given by an arbitrary function of the Ricci scalar R, of the trace of the energy–momentum tensor T, and of the matter Lagrangian $$L_m$$
L
m
, so that $$ L_{grav}=f\left( R,L_m,T\right) $$
L
grav
=
f
R
,
L
m
,
T
. We obtain the gravitational field equations in the metric formalism, the equations of motion for test particles, and the energy and momentum balance equations, which follow from the covariant divergence of the energy–momentum tensor. Generally, the motion is non-geodesic, and takes place in the presence of an extra force orthogonal to the four-velocity. The Newtonian limit of the equations of motion is also investigated, and the expression of the extra acceleration is obtained for small velocities and weak gravitational fields. The generalized Poisson equation is also obtained in the Newtonian limit, and the Dolgov–Kawasaki instability is also investigated. The cosmological implications of the theory are investigated for a homogeneous, isotropic and flat Universe for two particular choices of the Lagrangian density $$f\left( R,L_m,T\right) $$
f
R
,
L
m
,
T
of the gravitational field, with a multiplicative and additive algebraic structure in the matter couplings, respectively, and for two choices of the matter Lagrangian, by using both analytical and numerical methods.