U.S. stocks managed modest gains on Tuesday as better-than-expected corporate profits overshadowed concerns about weak consumer demand.

Analysts said the bulk of the day's news, including a surge in profit from Goldman Sachs , was already accounted for on Monday, when major stock indexes rose by more than 2 percent in anticipation of strong bank results.

But the cross-currents inherent in the midst of conflicting signals emerging from the earnings season kept the markets range-bound.

Gains were tempered by lackluster retail sales data and comments from Dell that second-quarter margins would be lower as demand has shifted toward cheaper computers, such as netbooks.

This was offset by encouraging comments from railroad company CSX Corp's chief executive and results from Johnson & Johnson that surpassed expectations.

The sellers had a chance to run with it, but again we found sideline cash ready to come in, said Scott Marcouiller, senior equity market strategist at Wells Fargo Advisors in St. Louis.

The bottom line is, we think we're still going to be in this trading range for a little while longer.

The Dow Jones industrial average <.DJI> added 27.81 points, or 0.33 percent, to 8,359.49. The Standard & Poor's 500 Index <.SPX> gained 4.79 points, or 0.53 percent, to 905.84. The Nasdaq Composite Index <.IXIC> rose 6.52 points, or 0.36 percent, to 1,799.73.

Data showed June retail sales increased 0.6 percent, which was more than forecast, but a big part of that gain was due to rising gasoline prices. Excluding autos and gas sales, retail sales registered a fourth consecutive monthly decline.

A rebound in sales is considered vital for the U.S. economy to bounce back from recession, as consumer spending accounts for roughly two-thirds of the country's economic activity.

Goldman Sachs Group's surge in quarterly profit handily beat expectations, but its stock gained just 0.2 percent to $149.66 after Monday's jump of 5 percent.

Johnson & Johnson's profit also surpassed forecasts and the Dow component rose 0.9 percent to $58.23.

The current earnings season is under particular scrutiny as investors look for signs of economic improvement.

Indeed, comments from the chief executive of CSX that the worst of the recession seems to be over helped bolster stocks.

CSX, which reported better-than-expected results after Monday's closing bell, saw its shares climb 7 percent to $34.80 on the New York Stock Exchange. The Dow Jones Transportation Average <.DJT> gained 1.3 percent.

But personal computer maker Dell limited gains after its lower margin forecast, pushing its stock down 8.1 percent to $11.97 on Nasdaq.

(Editing by Jan Paschal)